5

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 :\

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54

2 Answers2

2

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

  • thank's for the tip but it's not caused the crash, the real problem is the line above – Massimo Polimeni Jun 12 '15 at 12:00
  • 1
    mmm, that is odd indeed. Well another tip is to check that the setup is done correctly. For example: in my case when i specified the custom class, there the module field auto filled to something like `wktest_WatchKit_Extension` (i named my project wktest) and i see that in the screenshot that is not the case. If you can post the complete project somewhere i could have a look. Cheers and good luck. – Juan Carlos Ospina Gonzalez Jun 12 '15 at 12:18
  • no thank you, for now I've started a bounty and fix the for :) – Massimo Polimeni Jun 14 '15 at 09:56
  • 2
    I solved by myself but I decide to give my 50 points at the only person that tried to help me ;) – Massimo Polimeni Jun 15 '15 at 13:27
2

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 :)

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54