致命错误:在解开一个可选的值框架包装时意外发现nil(Swift)

时间:2015-08-28 18:46:21

标签: ios swift

我收到了以下示例的包装错误。不确定如何安全地包装这个,有什么建议吗?

   let frame = CGRectMake(10, 20, 300, 150)
        let pickerView = AKPickerView(frame : frame)
        //breaks here
        self.pickerView.delegate = self
        self.pickerView.dataSource = self

2 个答案:

答案 0 :(得分:0)

试试这个:

let frame = CGRectMake(10, 20, 300, 150)
if let pickerView = AKPickerView(frame : frame)
{
    self.pickerView.delegate = self
    self.pickerView.dataSource = self
}

答案 1 :(得分:0)

试试这个,在我的代码中进行测试

let frame = CGRectMake(10, 20, 300, 150)
let pickerView = AKPickerView(frame : frame)
pickerView.delegate = self
pickerView.dataSource = self
pickerView.reloadData()
self.view.addSubview(pickerView)

我使用了带有桥接头的Objective-c AKPickerView。