我是Swift的初學者,我發現這個代碼在線。我試着問作者,但他們不會回答,因爲我剛開始,如果有人能夠明確錯誤的確切原因以及我如何解決這個問題,我會非常感激。我繼續得到錯誤「致命錯誤:意外地發現零,同時解開可選值」。我嘗試在網上找到解決方案,但沒有奏效。有人請幫助我嗎?非常感謝! (有點解釋的將是巨大的!:))致命錯誤:意外地發現零,同時解開UIPickerView的可選值
這是我(以及他們的)代碼:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
@IBOutlet weak var pickerView: UIPickerView!
var pickerDataSource = ["White", "Red", "Green", "Blue"];
override func viewDidLoad() {
super.viewDidLoad()
self.pickerView.dataSource = self; // This is where the error occurs
self.pickerView.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerDataSource[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if(row == 0)
{
self.view.backgroundColor = UIColor.whiteColor();
}
else if(row == 1)
{
self.view.backgroundColor = UIColor.redColor();
}
else if(row == 2)
{
self.view.backgroundColor = UIColor.greenColor();
}
else
{
self.view.backgroundColor = UIColor.blueColor();
}
}
您是否將您的'pickerView'插座連接到您的故事板中的'UIPickerView'? – vacawama
確保您的IBOutlet未連接到故事板。 – NSGangster
我嘗試連接pickerView插座,但它只會重複行'@IBOutlet弱var pickerView:UIPickerView!'。 –