Post

Replies

Boosts

Views

Activity

Reply to "App development with swift" Apple pie guided project
It's a long time this question was asked, but I still had it. I hope the solution posted below will provide the help for future students :) What we know: the array guessedLetters contains the button titles, in other words: ONLY non-special characters. the value of word is a type String that can contain special characters the value of letter is a type character, that contains a unique character within word and it can be a special character What we want: the letter é being recognised as e (and the same for every special Character to be recognised as non-special character) the label displaying the word should display special characters as well What we will do: create a new constant nsLetter which will be the non-special character version of our variable letter check if this new constant is contained within the array guessedLetters example: add an accent to buccaneer so that it is buccanéer within the listOfWords array. the word is wrongly spelled, but we will use it as such only for the example. the code below should be the computed property "formattedWord" within the struct "Game":     var formattedWord: String {         var guessedWord = ""         for letter in word {             let nsLetter = Character(String(letter).folding(options: .diacriticInsensitive, locale: nil))             print(nsLetter)             /* after tests, you can comment the line above */ if guessedLetters.contains(nsLetter) {                 guessedWord += "\(letter)"             } else {                 guessedWord += "_"             }         }         return guessedWord     } More about this line: let nsLetter = Character(String(letter).folding(options: .diacriticInsensitive, locale: nil)) first step is to declare nsLetter to be same as letter: let nsLetter = letter problem: letter is a character and not a string. We can not use the .folding method on the character. we need to have a string: let's correct our code: let nsLetter = String(letter) now we just used the .folding method that we can found explained in the answer from danfromaustin above. let nsLetter = String(letter).folding(options: .diacriticInsensitive, locale: nil) so far nsLetter will contain only one letter but its type will be String and not Character. Problem: within the loop later, when checked if a string contains a character, if nsLetter is of Type String, the program will crash. lets just convert nsLetter to a Character Type by including its formal within the brackets of the expression: Character() which gives: let nsLetter = Character(String(letter).folding(options: .diacriticInsensitive, locale: nil)) now just use nsLetter instead of letter except in the guessedWord.
Jul ’20