2015-04-05 64 views
0

我試圖在Xcode v6.2中使用swift作爲一個小個人項目來構建iOS應用程序。到目前爲止,我已經拼湊了一些來自我自己的研究,但是當我嘗試構建時,下面的代碼會引發錯誤。在swift中構建完整的應用程序遠遠超出了我的知識範圍,所以我希望有人能夠引導我朝着正確的方向發展。Xcode中意外的編譯錯誤

目的只是爲了填充選擇器視圖,所以我沒有以任何方式綁定到代碼,如果有一些更簡單的邏輯。

下面是代碼:

import UIKit 
import iAd 
import QuartzCore 


class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{ 

@IBOutlet weak var yearPicker: UIPickerView! 
@IBOutlet weak var yearLabel: UILabel! 


    let listView = ["2013", "2014", "2015"] 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     println("Entering super.viewDidload() function") 
     yearPicker.delegate = self 
     yearPicker.dataSource = self 
    } 

    //Data Sources 
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
     return 1 
    } 
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return listView.count 
    } 

    //Delegates 
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 
     return listView[row] 
    } 

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     yearLabel.text = listView[row] 
    } 

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

我收到的錯誤是:

fatal error: unexpectedly found nil while unwrapping an Optional value

,並以下線的IDE強調:

yearPicker.delegate = self 

如果還有什麼可以幫助請讓我知道。

+3

您有IBOutlets。你是否正確連接了物品。錯誤消息非常明顯:在可選中發現無。該行中唯一可選的是yearPicker。它明確解開,所以它可能沒有正確設置。 – Abizern 2015-04-05 10:45:41

+0

乾杯,讓我走上了正確的軌道 - 我已經把我的完整答案留在下面 – 2015-04-05 11:38:31

回答

1

所以我結束了再次開始,通過ctrl +拖動到編輯器創建連接創建@IBOutlet連接。

然後我刪除.delegate.dataSource方法,因爲我收到一個錯誤,指出yearPicker沒有這些成員。所以現在我剩下這個代碼,這完美的作品:)

感謝Abizern把我放在正確的軌道上。

import UIKit 
import iAd 
import QuartzCore 


class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{ 

    @IBOutlet var yearPicker: [UIPickerView]! 
    @IBOutlet weak var yearLabel: UILabel! 

    let listView = ["2013", "2014", "2015"] 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    //Data Sources 
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
     return 1 
    } 
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return listView.count 
    } 

    //Delegates 
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 
     return listView[row] 
    } 

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     yearLabel.text = listView[row] 
     } 

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