Swift:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

时间:2016-06-16 21:41:48

标签: xcode swift

嗨,我是swift的新手,需要一些帮助

每次运行我的代码时,我的应用程序崩溃并吐出“上述错误

任何人都可以告诉我在代码中需要做什么或需要更换什么?

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{


    @IBOutlet weak var picker: UIPickerView!
    var employeeNames = ["John","Jane","Jack","James"]


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


        self.picker.delegate = self
        self.picker.dataSource = self
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return employeeNames.count
    }

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(employeeNames[row])
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }   
}

1 个答案:

答案 0 :(得分:1)

您的代码绝对正常,因此唯一的选择是您没有连接pickerView插座。按住Ctrl键单击故事板中的选择器视图,看看你的插座“选择器”是否显示出来。如果没有,则按住Ctrl键从pickerView拖动到你的插座以连接它。