Pass button title to function - nil while unwrapping optiona

Hello everybody,


I am using a button trigger in Playgrounds to pass the button title to a function in another class.


@objc func letterButtonPressed(_ sender: UIButton) {
        
        sender.isHidden = true
        hiddenButtons.append(sender)
        
        letter = sender.currentTitle?.uppercased()
        
        if letter != nil {
            game.checkLetterInWord(letter: letter!)
        }
}


In this function above I check if the letter is nil first, so I don't undertstand why the function finds nil...


public func checkLetterInWord(letter: String!) -> Bool {
        print(letter!)
        let wordCharacters = Array(word)
        print(wordCharacters)
        
        return true
}


What am I doing wrong? Thank you for you help!

Answered by OOPer in 418715022

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in line game.checkLetterInWord(letter: buttonLetter!)


I guess `game` is nil. Please show how `game` is declared and initialized.

I don't undertstand why the function finds nil...

How do you know it foond nil ?


Where do you call letterButtonPressed ?


You should show the complete code.


Note: no need to use String! here:

public func checkLetterInWord(letter: String!) -> Bool {

letterButtonPressed is a target for the button.


let newButton = UIButton(type: .system)
newButton.setTitle(alphabet[letter], for: .normal)
newButton.setTitleColor(UIColor.black, for: .normal)
newButton.addTarget(self, action: #selector(letterButtonPressed), for: .touchUpInside)


This is my current code:


@objc func letterButtonPressed(_ sender: UIButton) {
        
    sender.isHidden = true
    hiddenButtons.append(sender)
        
    let buttonLetter: String? = sender.currentTitle?.uppercased()
        
    if buttonLetter != nil {
        game.checkLetterInWord(letter: buttonLetter!)
    }
                
}


This is the function that receives the title of the button:


public func checkLetterInWord(letter: String) -> Bool {
    print(letter)
        
    return true
}


The error I get is:


Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in line game.checkLetterInWord(letter: buttonLetter!)

Which line do you get the error ?


The best would be to post the complete playground code, must not be that large.

Line 9 in the sencond block of code I posted:


game.checkLetterInWord(letter: buttonLetter!)
Accepted Answer

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in line game.checkLetterInWord(letter: buttonLetter!)


I guess `game` is nil. Please show how `game` is declared and initialized.

How is game defined ?

The best would be to post the complete playground code, must not be that large.

This was the problem... 😟

Thank you!!!

Good you solved it.


Next time, post the relevant information to begin with:

- which line the problem

- definition of different objects on that line


That will make it easier…

Pass button title to function - nil while unwrapping optiona
 
 
Q