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

Challenge 1 from Project5

Forums > 100 Days of Swift

I have the two conditions for the challenge declared in "isReal()" method I see that I'm already presenting an alert there, But I'm trying to do is to present two more alert for these conditions. Im currently getting this in my debud console and I'm still thinking where should I call that "showErrorMessage()" method. Could someone help me out with this bit please.

This is what I see in my debub console:

2021-04-05 19:44:54.734365+0100 Project5[3178:180576] [Presentation] Attempt to present <UIAlertController: 0x7ffc440c2c00> on <UINavigationController: 0x7ffc45817e00> (from <Project5.ViewController: 0x7ffc427080d0>) which is already presenting <UIAlertController: 0x7ffc440a7c00>.

func submit(_ answer: String) { let lowerAnswer = answer.lowercased()

    if answer.count == 0 {
        showErrorMessage(errorTitle: "No answer", errorMessage: "Please provide a valid answer")
        return
    }

    if isPossible(word: lowerAnswer) {
        if isOriginal(word: lowerAnswer) {
            if isReal(word: lowerAnswer) {

                userWords.insert(answer, at: 0)

                let indexPath = IndexPath(row: 0, section: 0)
                tableView.insertRows(at: [indexPath], with: .automatic)

                return
            }

            showErrorMessage(errorTitle: "Word not recognised", errorMessage: "You can't just make them up, you know!")
            return

        }

        showErrorMessage(errorTitle: "This word is already used!", errorMessage: "New word is required!")
        return
    }

    guard let title = title?.lowercased() else { return }
    showErrorMessage(errorTitle: "Word not possible", errorMessage: "You can't spell that word from \(title)")
    return

}

func isPossible(word: String) -> Bool { // Will check if the user is able to formuate a word
    guard var tempWord = title?.lowercased() else { return false }

    for letter in word { // Loops through the word provided by the player by checking and adding each letter to "letter" loop constant [This will contain all the letter from that word]

        if let positon = tempWord.firstIndex(of: letter) { // Checks if the player's word comtains letters from the start word [from what the player need to create a new word]

            tempWord.remove(at: positon) // Removes the used letter from the start word, that letter will not exist anymore so the player will not be able to use it twice
        } else {
            return false
        }
    }

    return true
}

func isOriginal(word: String) -> Bool { // Will check for duplicate words if any
    return !userWords.contains(word) // If the word is new this method will return "true", if the word was used before this method will return "false"
}

func isReal(word: String) -> Bool { // Will check is the word is a real word [english]
    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")

    if word.utf16.count < 3 {
        showErrorMessage(errorTitle: "Word to short", errorMessage: "Please provide a longer word")
        print("This word is to short!")
        return false
    }

    if title?.utf16.count == word.utf16.count {
        showErrorMessage(errorTitle: "Invalid answer", errorMessage: "You cannot submit the start word!")
        print("You can't submit the start word!")
        return false
    }

    return misspelledRange.location == NSNotFound // if misspelledRange.location == NSNotFound [true = not a valid word]; if misspelledRange.location == NSNotFound [false = we have a valid word

}

func showErrorMessage(errorTitle: String, errorMessage: String) {

    let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    present(alert, animated: true)
}

}

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.