Xcode中的意外构建错误

时间:2015-04-05 10:37:49

标签: ios swift

我正在尝试使用swift作为一个小小的个人项目在Xcode v6.2中构建一个iOS应用程序。到目前为止,我已经从我自己的研究中拼凑了一些内容,但是当我尝试构建时,以下代码会引发错误。在swift中构建完整的应用程序远远超出我的知识范围,所以我希望有一些明显的东西可以引导我朝着正确的方向发展。

目的只是填充一个选择器视图,所以如果有一些更简单的逻辑,我就不会以任何方式绑定代码。

以下是代码:

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.
    }
}

我收到的错误是:

  

致命错误:在解包可选值时意外发现nil

并在IDE中突出显示以下行:

yearPicker.delegate = self

如果还有其他任何可能有用的信息,请告诉我。

1 个答案:

答案 0 :(得分: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.
    }
}