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

SOLVED: Project 5 Word Scramble: count <3

Forums > SwiftUI

hi there

am having a probelm resolving the challenge for if the word count is < 3

I can get it to work, but the error message is the same regardless if the word count is < 3 or the word isn't a real word.

so the approach I took was to write else if and else clauses, in the guard aspect of the code. however, swift doesnt like this approach. Was hoping someone could provide guidance on how i can incorporate an else if and an if with guard, so that swift differentiates between the word being less than 3 characters or more, and not a real word.

here is the code i wrote. apologies but the code is not formatting correctly to read, perhaps due to the coding errors.

'''

guard isReal(word: answer) else if word.count < 3 { wordError(title: "Word not possible", message: "It is less than 3 characters") else { wordError(title: "Word not possible", message: "That isn't a real word") return } }

   // guard isReal(word: answer) else {
   //     wordError(title: "Word not possible", message: "That isn't a real word")
   //     return
  //  }

    usedWords.insert(answer, at: 0)
    newWord = ""
}

func isReal(word: String) -> Bool { guard word.count >= 3 else { return false

    }

    let checker = UITextChecker()
let range = NSRange(location: 0, length: word.utf16.count)
    let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")

    return misspelledRange.location == NSNotFound
}

'''

3      

apologies but the code is not formatting correctly to read, perhaps due to the coding errors.

When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Doing this will ensure that you end up with something like this:

func printSomething(_ thing: String?) {
    if let thing = thing {
        print(thing)
    } else {
        print("nothing there")
    }
}

instead of this:

func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } }

It looks like you tried to do this, but you used single quotes ''' instead of backticks ```. At least on an English keyboard, the backtick is located to the left of the 1 key along the top.

3      

And on to your issue...

guard statements don't work the way you are trying to make them work. Instead, you need to use multiple guard statements, each returning something different or doing something different in the event of failure.

So, something like:

guard isReal(word: answer) else {
    wordError(title: "Word not possible", message: "That isn't a real word")
    return
}

guard word.count >= 3 else {
    wordError(title: "Word not possible", message: "It is less than 3 characters")
    return
}

///etc...

Then after you've made it past all the guard statements, you know the user has entered a valid answer.

3      

thanks

really appreciate the help with guard, and with the 3 backticks.

thanks for the patience!

Paul

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.