I'm attempting to build an iOS app in Xcode v6.2 using swift as a little personal project. So far I've cobbled together bits from my own research, however the following code throws an error when I try to build. Building full apps in swift is well out outside my knowledge scope so I'm hoping it's something obvious that someone can kindly guide me in the right direction.
The purpose is simply to populate a picker view, so I'm not in any way bound to the code if there is some simpler logic.
Here is the code:
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.
}
}
The error I'm receiving is:
fatal error: unexpectedly found nil while unwrapping an Optional value
and it the following line is highlighted in the IDE:
yearPicker.delegate = self
If there's anything else that might help please let me know.
So I ended up starting again, creating the connection by ctrl + dragging into the editor to create the @IBOutlet
connection.
I then removed the .delegate
and .dataSource
methods because I received an error stating that yearPicker did not have those members. So now I'm left with this code which works perfectly :)
Thanks to Abizern for putting me on the right track.
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.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.