Post

Replies

Boosts

Views

Activity

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I am a beginner to swift language and with the help of apple book, I was typing this code on switching colours using buttons. then in this line: if redswitch.isOn { it is showing this fatal error. What to do I have no idea. Please help me out. Here's the code: import UIKit class ViewController: UIViewController { @IBOutlet weak var colourView: UIView! @IBOutlet weak var redswitch: UISwitch! @IBOutlet weak var greenswitch: UISwitch! @IBOutlet weak var blueswitch: UISwitch! override func viewDidLoad() { super.viewDidLoad() updateColor() // Do any additional setup after loading the view. } @IBAction func switchChanged(_sender: UISwitch){ updateColor() } func updateColor() { var red: CGFloat = 0 var green: CGFloat = 0 var blue: CGFloat = 0 if redswitch.isOn { red = 1 } if greenswitch.isOn { green = 1 } if blueswitch.isOn { blue = 1 } let color = UIColor(red: red, green: green, blue: blue, alpha: 1) colourView.backgroundColor = color } }
2
0
303
Jan ’24