-1

I am trying to pass my custom struct ILT through pushControllerWithName() so I can pass it to the next screen in my watchOS 2 app. ILTList is an array of ILT types, and the pushControllerWithName() is happening when the user taps on a row in the table, which has its' rows fed from ILTList.

ILTList is initialised with var ILTList = [ILT](), and the ILT struct is as follows:

struct ILT {
    let homeworkID: Int
    let title: String
    let subject: String
    let teacher: String
    let teacherCode: String
    let studentID: Int
    let description: String
    let due: Double
    let status: String
    let hasAttachments: Bool
}

And this is where the tap occurs:

override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    print(ILTList[rowIndex])
    let context:AnyObject = ILTList[rowIndex]
    self.pushControllerWithName("showILT", context: context)
}

With this code I get

Cannot subscript a value of type '[ILT]'

When I change context:AnyObject to just context or context:ILT, it says:

Cannot convert value of type 'ILT' to expected argument type 'AnyObject'?

What am I missing here?

James
  • 1,088
  • 3
  • 11
  • 29

2 Answers2

0

For people who really want to use pushControllerWithName(), I formed the following solution by converting my ILT variable type to a Dictionary.

override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    // construct the dictionary
    let context = [
        "title":ILTList[rowIndex].title,
        "subject":ILTList[rowIndex].subject,
        "teacher":ILTList[rowIndex].teacher
    ]
    self.pushControllerWithName("showILT", context: context)
}
James
  • 1,088
  • 3
  • 11
  • 29
0

That's a weird error message. I think the problem might be that pushControllerWithName:context: takes a context of type AnyObject?, and structs can't be passed for that type. A dictionary works because it is a class-type object.

Evan
  • 2,282
  • 2
  • 19
  • 21