Search code examples
iosswiftuipickerview

Constantly getting this error: Thread 1: EXC_BAD_INSTRUCTION(code=EXC_l386_INVOP,subcode=0x0)


I constantly get this error and I really don't know why as the application is rather simple and I built it upon a tutorial on YouTube where everything seemed to working properly. (The tutorial was written in Swift 2, however and I'm already using Swift 3) Can anyone help me, please?


import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    @IBOutlet var image1: UIImageView!

    @IBOutlet var image2: UIImageView!

    @IBOutlet var label1: UILabel!

    @IBOutlet var label2: UILabel!

    @IBOutlet var message: UILabel!

    var data = [["USA", "Italy", "China", "England"], ["Beijing", "London", "Rome", "Washington, DC"] ]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return data.count // Based on data. No hardcoded number.
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return data[component].count

        // For each component (we have 2 components in our array -> the two elements (Countries = USA, Italy, China, England & Capitals = Beijing, London, Rome, Washington) grab the count for the row.

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return data[component][row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        let item1 = data[0][pickerView.selectedRow(inComponent: 0)]
        let item2 = data[1][pickerView.selectedRow(inComponent: 1)]

        print(item1)
        print(item2)

        // Grab whatever is selected in index [0] or index [1] into "selectedRow" contrast each other and compare if country and capital match each other.

        // Now we refer to the images so that we don't have to type in the exact name over and over again. 

        let usa = UIImage(named: "USA.png")
        let italy = UIImage(named: "Italy.jpeg")
        let china = UIImage(named: "China.jpeg")
        let england = UIImage(named: "England.jpeg")

        let washington = UIImage(named: "Washington.jpeg")
        let rome = UIImage(named: "Rome.jpeg")
        let beijing = UIImage(named: "Beijing.jpeg")
        let london = UIImage(named: "London.jpeg")

        let correctMessage = "The Capital of \(item1) is \(item2)"
        let wrongMessage = "Match the flags to the Capitals."


        switch item1 {

            case "USA":

                image1.image = usa
                label1.text = "USA"

            case "Italy":

                image1.image = italy
                label1.text = "Italy"

            case "China":

                image1.image = china
                label1.text = "China"

            case "England":

                image1.image = england
                label1.text = "England"

        default:
                image1.image = usa
                label1.text = "USA"

        } // End Of Switch 1

        switch item2 {

            case "Beijing":

                image2.image = beijing
                label2.text = "Beijing"

                if label1.text == "China" {

                    message.text = correctMessage

                }

                else {

                    message.text = wrongMessage

            }

            case "London":

                image2.image = london
                label2.text = "London"

                if label1.text == "England" {

                    message.text = correctMessage

            }

                else {

                    message.text = wrongMessage

            }

            case "Rome":

                image2.image = rome
                label2.text = "Rome"

                if label1.text == "Italy" {

                    message.text = correctMessage

            }

                else {

                    message.text = wrongMessage

            }

            case "Washington, DC":

                image2.image = washington
                label2.text = "Washington, DC"

                if label1.text == "USA" {

                    message.text = correctMessage

            }

                else {

                    message.text = wrongMessage

            }

        default:
                image2.image = washington
                label2.text = "Washington, DC"

        } // End Of Switch 2




    } // End Of Did Select Row

}  // End Of ViewController | End Of App

Solution

  • I'm guessing that this crash is coming from your use of implicitly unwrapped options, those ! variables at the top. These are variables that are allowed to be nil, but will cause a crash if accessed when nil.

    First, allow the app to stop in the debugger, so you can see which line is responsible. Then, verify that you have everything hooked up correctly in Interface Builder. My guess is one or more of those @IBOutlet variables is not correctly wired up.