NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Problem: Day 37 trimmingCharacters

Forums > 100 Days of Swift

When I remove trimmingCharacters from below line, I see no different when printing cluesLabel.text in console or in simulator .

cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)

So why we should remove the new line \n from clueString?

   

Are you sure you don't see any difference? Not even a subtle one?

Here's what mine looks like with trimmingCharacters(in:)...

with trimming

And here's what it looks like without trimmingCharacters(in:)...

without trimming

See the subtle difference?

And in the console, with trimmingCharacters(in:)...

console with trimming

And without trimmingCharacters(in:)...

console without trimming

Here you can see there's an extra line after the non-trimmed version.

   

It is interesting, in your simulator screenshot, I could see the last line is different in horizontal alignment. And there is an external line in console. However I couldn't see those two difference in my side. Below is what I got.

Here is the log in console:

And in simulator without trimmingCharacters():

with trimmingCharacters():

The code:

func loadLevel() {
        var clueString = ""
        var solutionString = ""
        var letterBits = [String]()

        if let levelFileURL = Bundle.main.url(forResource: "level\(level)", withExtension: "txt") {
            if let levelContents = try? String(contentsOf: levelFileURL) {
                var lines = levelContents.components(separatedBy: "\n")
                lines.shuffle()

                for (index, line) in lines.enumerated() {
                    let parts = line.components(separatedBy: ": ")
                    let answer = parts[0]
                    let clue = parts[1]

                    clueString += "\(index + 1). \(clue)\n"

                    let solutionWord = answer.replacingOccurrences(of: "|", with: "")
                    solutionString += "\(solutionWord.count) letters\n"
                    solutions.append(solutionWord)

                    let bits = answer.components(separatedBy: "|")
                    letterBits += bits
                }
                print("clueString:\n\(clueString)")
            }
        }

        // Configure buttons and labels
        cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)
        print("withTrimm:\n\(cluesLabel.text!)")
        answersLabel.text = solutionString.trimmingCharacters(in: .whitespacesAndNewlines)

        letterBits.shuffle()

        if letterBits.count == letterButtons.count {
            for i in 0 ..< letterButtons.count {
                letterButtons[i].setTitle(letterBits[i], for: .normal)
            }
        }
    }

   

Well, I can't see your images, but when I paste your loadLevel function into my project, I see this in the console:

console output

That blank line between the two groups is the extra \n from not trimming.

And I don't see the baseline discrepancy in the simulator because your code trims the string before assigning it to cluesLabel:

cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)
print("withTrimm:\n\(cluesLabel.text!)")

But if I comment out the call to trimmingCharacters(in:) then I do see the lines not matching up in the simulator, as I would expect to see.

So I'm a little confused what the issue here is. Maybe if I could see your screenshots I might understand better.

   

Well, there is another print under the for loop as I want to see the result withTrim and withoutTrim. I could see it in console it has two string printed out there.

I could see the screenshot from here and don't know why you cannot see it.

WithTrim: print("clueString:\n\(clueString)")

WithoutTrim: print("withTrimm:\n\(cluesLabel.text!)")

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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.