WKInterfaceTable is not getting populated with Codable data












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...
}









share|improve this question

























  • 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 4: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 4: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 4:30
















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...
}









share|improve this question

























  • 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 4: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 4: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 4:30














0












0








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...
}









share|improve this question
















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...
}






swift codable watchconnectivity wcsession wkinterfacetable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 4:33







TokyoToo

















asked Nov 19 '18 at 3:20









TokyoTooTokyoToo

5731716




5731716













  • 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 4: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 4: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 4:30



















  • 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 4: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 4: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 4:30

















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 4:31





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





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 4:07





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 4: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 4:30





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 4:30












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367872%2fwkinterfacetable-is-not-getting-populated-with-codable-data%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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






    share|improve this answer




























      0














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






      share|improve this answer


























        0












        0








        0







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






        share|improve this answer













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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 4:37









        TokyoTooTokyoToo

        5731716




        5731716






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367872%2fwkinterfacetable-is-not-getting-populated-with-codable-data%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?