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 :)
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
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":
Code Block 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 |
} |
Code Block let nsLetter = Character(String(letter).folding(options: .diacriticInsensitive, locale: nil)) |
first step is to declare nsLetter to be same as letter:
Code Block 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:
Code Block let nsLetter = String(letter) |
now we just used the
.folding method that we can found explained in the answer from
danfromaustin above.
Code Block 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:
which gives:
Code Block let nsLetter = Character(String(letter).folding(options: .diacriticInsensitive, locale: nil)) |
now just use
nsLetter instead of
letter except in the
guessedWord.