0

My JSON object is not getting sent to my WKInterfaceTable.

UITableView is populating correctly. Last step is to populate the WKInterfaceTable. The code is not throwing an errors in Xcode but there is a runtime crash because the cast is failing.

Edit: Thanks to Dale I fixed an error handling line so the run-time message is gone, but the same problem persists.

printing loadedData: Optional(<5b7b2273 7065616b 6572223a 224a6f68 6e222c22 7469746c 65223a22 486f7720 61726520 796f753f 222c2274 6f223a22 323a3030 222c2266 726f6d22 3a22313a 3030227d ...>)

    override func willActivate() {
         super.willActivate()

        if(WCSession.default.isReachable) {

            let message = ["getMsgData" : [:]]

            WCSession.default.sendMessage(message, replyHandler:

                { (result) -> Void in

                    print("Requesting data from phone")
                    print("printing message: \(message)")
                    print("printing messageObjects: \(self.messageObjects)")

                    if result["messageData"] != nil { 
                        let loadedData = result["messageData"]

                        NSKeyedUnarchiver.setClass(MessageObject.self, forClassName: "MessageObject")

                        do {
                            let loadedPerson = try? JSONDecoder().decode(MessageObject.self, from: loadedData as! Data)

                            self.messageObjects = [loadedPerson]
                            self.progTable.setNumberOfRows(self.messageObjects.count, withRowType: "MsgRowController")

               //code...
    }
TokyoToo
  • 926
  • 2
  • 10
  • 21
  • The cast will fail if loadedPerson is nil i.e. if decode fails. Instead of using try? you should handle the error properly. – Dale Nov 19 '18 at 04:31
  • Thanks. Changed it to `let loadedPerson = try JSONDecoder().decode` and then `self.messageObjects = [loadedPerson]`. I noticed that `print("Sending a message to the phone asking it for data")` in **MessageInterfaceController** is printing to the console, but the WKInterfaceTable/rows still aren't displaying. So something in `willActivate` is definitely wrong. No more runtime crashes but table data isn't displaying. – TokyoToo Nov 19 '18 at 12:26
  • You should debug your JSON decoding separately. The error message is really telling you the problem - it can't parse the JSON because it isn't in the correct format. It looks like your JSON is an array but you are asking to decode a single object. Try using [MessageObject].self as the type in JSONDecoder().decode. loadedPerson will then be an array which you can assign to self.messageObjects directly without enclosing in [ ] – Dale Nov 20 '18 at 04:07
  • Hi Dale. Actually I just refactored my Codable model and while I was doing that I found out the problem was I had previous NSKeyedArchiver/Unarchiver methods in both the VCs so it was basically stalling my app because it didn't know whether to use NSKeyed or JSONDecoder. Xcode gave no errors, no warnings nothing. I just had to do some refactoring to find out the issue on my own. Thanks for your tips. While I did that Xcode gave me warnings to change the areas you mentioned as well. – TokyoToo Nov 20 '18 at 04:30

1 Answers1

0

Through the process of updating to using the Codable protocol I didn't realize I had to remove previous instances of NSKeyedArchiver/Unarchiver.

TokyoToo
  • 926
  • 2
  • 10
  • 21