Apple Pie Guided Project: getting error - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

Hi, I am working on the apple pie project trying to figure out why this line of code is outputting an error. all of my connections are correct and I have double checked them and the game file is correct as well. how can I remove this error and continue the project? [

Replies

When you show your code, please show it as text using Code Block.


I do not know much about Apple Pie Guided Project, but is it written for the version of Xcode you are using?

Apple introduced drastic changes to UIButton in iOS 15, so some of the code once worked in old Xcodes, does not work in Xcode 13.

One possible way to fix this issue, is getting the right version of Xcode which your Apple Pie Guided Project is targeting. More Downloads

Another possible try, though I'm not sure if this works well in your case, would be changing the Project Format compatible to an old one.


If you want to learn what's changed in iOS 15 rather than the basics of app development, there may be some fix just touching some parts of your code. But, as you are not showing your code as text, I cannot retrieve needed text from your screen shot.

So the issue is that your UIButton does not have a title, so title(for:) returns nil. You could work around the error by doing something like let letterString = sender.title(for: .normal) ?? "", but I think that will just cause errors later down the line. Try using the currentTitle API if you haven't already. Also, where do you set the titles of your letterButtons? I don't see anything in the screenshots.

let letterString = sender.title(for: .normal)! no longer works as of the IOS15 update. You can do this: let letterString = sender.titleLabel!.text! or this: let letterString = sender.configuration!.text! to fix the issue. But you should probably be using a guard here. guard let letterString = sender.configuration?.text else { return print("error unwrapping button title") } That way the app doesn't crash (it still doesn't work, but it doesn't crash, so you're moving in the right direction).

Hi ashtonlalchan,

I'm working through the same project and ran into the same issue. I got past it by using the titleLabel property which returns a UILabel. Then I used the UILabel's text property. This is what my letterButtonPressed function looks like:

@IBAction func letterButtonPressed(_ sender: UIButton) {
        sender.isEnabled = false
        let letterString = sender.titleLabel?.text
        let letter = Character(letterString!.lowercased())
        currentGame.playerGuessed(letter: letter)
        updateUI()
    }
  • I was having the same issue and this fixed it! Also, I had to add, I had to add "!" after letterButton.

    func enableLetterButtons(_ enable: Bool) {
        for button in letterButtons! {
            button.isEnabled = enable
        }
    }
    

    I hope this helps anyone that went through this issue!

Add a Comment