1

I am trying to create a WatchKit picker in Xcode to let the user choose number of laps (1 to 10). However, after creating the items I get a fatal error when trying to put them in the picker.

"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

//sets the items 1 to 10 to lap picker
func setPickerItems(){
    var pickerItems: [WKPickerItem] = []

    for i in 1...10{
        print(i)
        let item = WKPickerItem()
        item.title = String(i)
        pickerItems.append(item)
    }

    //Here I get the error
    lapPicker.setItems(pickerItems)
}

@IBOutlet weak var lapPicker: WKInterfacePicker!

What can I do to fix it in this function? I'm new to Swift and WatchKit.

Thank you!

Martin S
  • 11
  • 2
  • 1
    What is `lapPicker`? I guess it's nil. Is that a `IBOutlet`? Did you connect it in Interface? – Larme Oct 12 '18 at 13:24
  • Thank you for asking the right questions! It works now! It was an IBOutlet, however it did not have a connection to the interface for some reason (the circle left to the IBOutlet wasn't filled), maybe because I wrote the outlet manually in my controller? I ctrl-dragged it to the controller instead, which did the trick. Thanks again! – Martin S Oct 12 '18 at 13:37
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Larme Oct 12 '18 at 13:38
  • Well, maybe. I read the post, couldn't understand it enough to apply it to my problem though. The problem was in my inability to crate an Outlet connection. – Martin S Oct 12 '18 at 13:41
  • On this answer: https://stackoverflow.com/a/52399160/1801544 It's the 4 point. The reason it's that `lapPicker` is `nil`. The rest depend on the context. – Larme Oct 12 '18 at 13:42

0 Answers0