1 Reply
      Latest reply on Apr 22, 2019 6:33 AM by John368
      Volker88 Level 1 Level 1 (0 points)

        Hello,

         

        i'm currently working through the book Intro to app development with Swift. At the moment i'm working on Lesson 17.3: Multiple Actions & Outlets.

         

        I can built the app and run it in the simulator. But as soon as i want to turn on one switch i receive an error.

        Unfortunately i can only find the solution for the final code of this project.

         

        Maybe someone could help me understand & solve this error.

         

        Error Code:

        Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
        2019-04-13 20:16:58.584388+0200 Color_Mix[58605:3227076] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
        (lldb)
        

         

        Main.Storyboard:

        import UIKit
        
        class ViewController: UIViewController {
        
            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
                colorView.backgroundColor = .black
            }
        
            @IBOutlet weak var colorView: UIView!
            
            @IBOutlet weak var redSwitch: UISwitch!
            @IBOutlet weak var greenSwitch: UISwitch!
            @IBOutlet weak var blueSwitch: UISwitch!
            
            @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)
                colorView.backgroundColor = color
            }
        }