On/Off Button Title in updateUI()

I am working through the Getting Started with App Development and on Page 142 it has you write in the updateUI() function if else statement lightButton.setTitle("Off", for: .normal) and lightButton.setTitle("On", for: .normal). My app crashes every time after building it and running it. Image reference below. The app works perfectly fine before adding these two statements.

import UIKit

class ViewController: UIViewController {

@IBOutlet var lightButton: UIButton!

var lightOn =  true

fileprivate func updateUI() {
    if lightOn {
        view.backgroundColor = .white
        lightButton.setTitle("Off", for: .normal)
    } else {
        view.backgroundColor = .black
        lightButton.setTitle("On", for: .normal)
    }
}

@IBAction func buttonPressed(_ sender: Any) {
    lightOn.toggle()
    updateUI()
}
override func viewDidLoad() {
    super.viewDidLoad()
    updateUI()
    // Do any additional setup after loading the view.
}

}

Getting this error at the end. libc++abi: terminating due to uncaught exception of type NSException (lldb)

I tested your code, no crash.

Xcode 14.2, iOS 16.2 simulator.

Which versions do you use ?

That's not the cause of your problem, but I usually define the exact type of parameter:

@IBAction func buttonPressed(_ sender: UIButton) { }

What is the button type ? System ? custom ? Check in Attributes Inspector.

I was using Xcode 14.3, iOS 16.2 simulator. I have just updated to Sonoma and have Xcode 15.0, iOS simulator 15. Button type is System. Even in Xcode 15 I still have this issue.

Now on the AppDelegate screen where it shows @main I get an X Expected Expression next to it.

//  AppDelegate.swift
//  Light
//
//  Created on 6/28/23.
//

import UIKit

∞@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

I got rid of that infinity symbol in front of @main and it got rid of that error and would actually build and run. But now I run into another issue on the AppDelegate screen.

The simulator launches but the button letters don't appear and I cannot click the Button to change the screen.

On/Off Button Title in updateUI()
 
 
Q