Search code examples
swiftwatchkit

WKinterfacePicker item issue


I have a WKInterfacePicker in my Watch's Interface and want to add items to it. Here is my code I currently have:

let item1:WKPickerItem = WKPickerItem()
item1.title = "1"

let item2:WKPickerItem = WKPickerItem()
item2.title = "2"

timePicker.setItems([item1, item2])

When I run the app it crashes and tells me there is a nil last line:

fatal error: unexpectedly found nil while unwrapping an optional value

I had a look at Apple's docs for the WKInterfacePicker and WKPickerItem but that did not help me either. Is it possible that I have to set the content of the items somehow else? I thought the title is the content already and I couldn't find any other method for setting content.


Solution

  • It's likely that timePicker is nil. Did you remember to hook it up to the outlet?

    @IBOutlet var timePicker: WKInterfacePicker!
    

    Since it's declared with !, this is the only place you could be force unwrapping nil.

    If you're still having problems, there's a full guide on WKInterfacePicker you can try here: http://www.sneakycrab.com/blog/2015/6/12/wkinterfacepicker-in-watchkit-20-using-the-digital-crown