WKInterfaceTable & setNumberOfRows get crash "unexpectedly found nil while unwrapping an Optional value"

993 views Asked by At

I'm working with WatchKit, I have a simple interface with only a table but I get an error with just a few lines of code, maybe I forgot something really basic.

My interface:

the row inside the table has the identifier:

and the custom class:

The controller is implemented by this code:

import WatchKit
import Foundation

class ActiveListController: WKInterfaceController
{
    @IBOutlet weak var tableView: WKInterfaceTable!

    override func awakeWithContext(context: AnyObject?)
    {
        super.awakeWithContext(context)

        loadData()
    }

    override func willActivate()
    {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate()
    {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    func loadData()
    {
        tableView.setNumberOfRows(10, withRowType: "ItemRow") // GET ERROR

        for index in 0...9
        {
            let row = tableView.rowControllerAtIndex(index) as! ItemRow

            row.nameLabel.setText("test")
        }
    }

}

and obviously I have my custom class for the single row

import Foundation
import WatchKit

class ItemRow : NSObject
{
    @IBOutlet weak var checkImage: WKInterfaceImage!
    @IBOutlet weak var nameLabel: WKInterfaceLabel!
}

So when I run the app I get error when I try to set the number of rows but really I can't understand what is nil:

fatal error: unexpectedly found nil while unwrapping an Optional value

Maybe it's a simple mistake, maybe not but please help me :\

2

There are 2 answers

0
Massimo Polimeni On BEST ANSWER

finally I found my mistake.

I forgot to set my only one interface for Apple Watch as Initial Controller.

Yep, unbelievable and embarrassing but that's it. The error that provides Xcode it's not the best , it would be better something like "initial controller missing".

I hope my question & answer can help someone one day :)

4
Juan Carlos Ospina Gonzalez On

The for loop in your code is going from 1...10 and it should be 0...9 because the rows are 0 based