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

Problem: Day 37 trimmingCharacters

Forums > 100 Days of Swift

@Haibo  

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?

3      

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.

3      

@Haibo  

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)
            }
        }
    }

3      

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.

3      

@Haibo  

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!)")

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.