"App development with swift" Apple pie guided project

Hi everyone.


I've finished all apple pie challenges recently, but faced with this one:


- Support letters with special characters. For example, the E button could check for "e" and "é" within a word.


Could anyone who has passed this assist with some help? Thanks!

Answered by KMT in 289226022
Accepted Answer

Another option I found was to use stringByFoldingWithOptions. The documentation notes "...it’s often useful to compare strings in such a way that ignores differences in case (uppercase or lowercase), width (full-width or half-width), and/or diacritics (accents and other marks)".


Example how to use is at https://stackoverflow.com/questions/29521951/how-to-remove-diacritics-from-a-string-in-swift

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":

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
    }



More about this line:

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:
Code Block
Character()

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.

I am working on the Apple Pie Project now,
I followed the code on the book and so far it works well, (except the .character method can not be used)
but at the last step I found an issue which X code said:

Value of type 'UIButton' has no member 'isEnable'

on this code block:
Code Block func enableLetterButtons(_ enable: Bool){
    for button in letterButtons {
      button.isEnable = enable
    }


any one has also faced this issue?

I have make sure I am using Outlet not Action

Code Block @IBOutlet weak var treeImageView: UIImageView!
   
  @IBOutlet weak var correctWordLabel: UILabel!
   
  @IBOutlet weak var scoreLabel: UILabel!
   
  @IBOutlet var letterButtons: [UIButton]!
   
   
  @IBAction func buttonPressed(_ sender: UIButton) {
    sender.isEnabled = false
    let letterString = sender.title(for: .normal)!
    let letter = Character(letterString.lowercased())
    currentGame.playerGuessed(letter: letter)
    updateGameState()
  }





I found the answer of my question.. (but I can't edit or delete my question....)

"isEnabled" , not "isEnable"

"App development with swift" Apple pie guided project
 
 
Q