Cannot decode object of class

后端 未结 5 1673
难免孤独
难免孤独 2020-12-08 04:54


I am trying to send a \"Class\" to my Watchkit extension but I get this error.

* Terminating app due to uncaught exception \'NSInvalidUna

5条回答
  •  醉话见心
    2020-12-08 05:23

    NOTE: While the information in this answer is correct, the way better answer is the one below by @agy.

    This is caused by the compiler creating MyApp.Person & MyAppWatchKitExtension.Person from the same class. It's usually caused by sharing the same class across two targets instead of creating a framework to share it.

    Two fixes:

    The proper fix is to extract Person into a framework. Both the main app & watchkit extension should use the framework and will be using the same *.Person class.

    The workaround is to serialize your class into a Foundation object (like NSDictionary) before you save & pass it. The NSDictionary will be code & decodable across both the app and extension. A good way to do this is to implement the RawRepresentable protocol on Person instead.

提交回复
热议问题