shjnlee shjnlee - 6 months ago 43
iOS Question

Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

I'm pretty new to swift (I usually code in Java and C++), and I'm building my first ios app.
In this app, I'm trying to create a picker view, and the problem is when I'm trying to set the initial value of the label point to the first item in the picker view (array) it pops up the error


"fatal error: unexpectedly found nil while unwrapping an Optional
value"


Here is my code I'm working

class ViewController: UIViewController, UIPickerViewDelegate{


@IBOutlet var usertypeLabel: UILabel!
@IBOutlet var Picker: UIPickerView!
var usertype_array = ["patient","nurse"]
//var usertype: String!
override func viewDidLoad() {

// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
}

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

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return usertype_array.count
}

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var rowselected = usertype_array[row]
usertypeLabel.text = rowselected
}
}


Any idea why the error happens? Note that it stills runs if I delete that line of code. Thanks.

Answer

You are missing UIPickerViewDataSource and add it here:

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource

And in your viewDidLoad method add this:

Picker.dataSource = self
Picker.delegate = self

And here is your full working code:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{


    @IBOutlet var usertypeLabel: UILabel!
    @IBOutlet var Picker: UIPickerView!
    var usertype_array = ["patient","nurse"]
    //var usertype: String!
    override func viewDidLoad() {

        // Do any additional setup after loading the view, typically from a nib.
        super.viewDidLoad()
        Picker.dataSource = self
        Picker.delegate = self
        usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
    }

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

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return usertype_array.count
    }

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
        var rowselected = usertype_array[row]
        usertypeLabel.text = rowselected
    }
}

If your outlet is correct it will work fine.

Check THIS sample for more Info.

Comments