UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 13 - print(quote.trim()) outputs "()"

Forums > 100 Days of SwiftUI

I am following along the code on Day 13 (Protocols and Extensions) and this code is confusing me:

var quote = "   The truth is rarely pure and never simple   "

extension String {
    func trimmed() -> String {
        self.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    mutating func trim() {
        self = self.trimmed()
    }
}

print(quote.trim()) // this is outputting "()"

(I've removed the extra code from the example to illustrate my issue). In the above, quote.trim() seems to be fine as the right sidebar in playground shows the expected text. But then when I print it, I just get ()\n. Have I done something wrong? Thanks for any help.

2      

Welcome to the forum! Great job getting almost to the 14 day milestone. I believe you're supposed to trim it as you did with quote.trim then print (quote)....Here's how it shoud look:

var quote = "   The truth is rarely pure and never simple   "

extension String {
    func trimmed() -> String {
        self.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    mutating func trim() {
        self = self.trimmed()
    }
}
quote.trim()
print(quote) // The truth shall set you free...:).

4      

In the above, quote.trim() seems to be fine as the right sidebar in playground shows the expected text. But then when I print it, I just get ()\n. Have I done something wrong?

The problem is that quote.trim() does not return a value, i.e., it returns Void. In Swift, Void is defined as an empty tuple (). So that's what you're getting in the print statement.

In other words, this:

mutating func trim() {
    self = self.trimmed()
}

is the same as this:

mutating func trim() -> Void {
    self = self.trimmed()
}

is the same as this:

mutating func trim() -> () {
    self = self.trimmed()
}

and the print is getting that () output from trim and dumping it into the console.

Make sense?

So the correct way to do what you are attempting would be either to use the mutating function on quote and then put quote into a print, like so:

quote.trim()
print(quote)

or use the trimming function, which returns a String:

print(quote.trimming())

The difference being, of course, that using trimming does not alter the value of quote but returns a new String that is the same as quote but with the whitespace stripped out. quote still has the same value it had before you called trimming. Whereas by using trim, you are altering the value of quote so that when you use it later, it will have that altered value rather than the original value.

5      

Thank you @roosterboy and @GeorgeMCSwifter for such helpful responses! That makes sense to me now. I'd not considered that the trim() method was mutating and not returning the string.

Much appreciated!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.