NEW: Learn to build the incredible iOS 15 Weather app today! >>

Day 31 Completed

Forums > 100 Days of SwiftUI

I finished day 31. For some reason my rootWords were coming in as having one more character than they should have had. I don't remember having this problem the first time I did this app. It showed up as a problem when I was checking to see if someone used the rootWord as their guess for the new word.

I was able to fix it by adding the .trimmingChareacters(in: .whitespacesAndNewlines). I checked Paul's code and didn't see it in there, but mine seemed to need it.

if let startWordsUrl = Bundle.main.url(forResource: "start", withExtension: "txt") {
            if let startWords = try? String(contentsOf: startWordsUrl) {
                let allWords = startWords.components(separatedBy: "\n")
                rootWord = allWords.randomElement()?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "silkworm"
                return
            }
        }

   

What did your code look like before adding .trimmingCharacters() method?

// Your code
rootWord = allWords.randomElement()?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "silkworm"     
// My code
// Pick a random array element to be the rootWord
rootWord  = allWords.randomElement() ?? "terrific"

Did you use an older version of the start.txt file? Maybe there are stray characters there?

Because these are all 8 character words, have you tried assert() as a debugging tool?

See this article and video-> Debugging with Assert

Assert takes two parameters.

  1. The statement you are checking.
  2. The message that's printed when the statement fails.

Try this in your code:

rootWord = allWords.randomeElement() ?? "terrific"
assert( rootWord.count == 8 , "\n*** \(rootWord) length is not eight!")

   

My code looked similar to your last line of code.

The first time I did the WordScramble app, I manually browsed to the start.txt file and downloaded it using a web browser. The second time around, I downloaded some Windows software from github that copied all the project files to a folder so I wouldn't have to keep going back and downloading stuff. Maybe something happened in the transfer. As far as I can tell the file looks fine, but every word definitely had an extra character in it because when I would do a rootWord.count, they were coming out with one extra character even though a visual count only showed the right amount.

   

So, I went back and checked and start.txt from my first download and first time building the app was only 111kb. My second download and second time building the app, start.txt is 124kb. So, apparently, something is different between the two.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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.