0

I am transferring data from my iOS app to its Watch extension via the application context. I want to send a custom object I've created (named WeatherReport).

let context = ["report" : WeatherReport]
WCSession.defaultSession().updateApplicationContext(context)

However, I get the following error:

Value of type WeatherReport does not conform toe expected dictionary value type 'AnyObject'

I am wondering why I am unable to set my custom object as a value in the dictionary I am trying to pass as the applicationContext.

4

1 回答 1

1

即使您可以通过编译器错误,您也会收到运行时错误。WCSession 字典只能包含属性列表类型,它们只是字符串、数字、数据等基本类型。

如果你真的想发送你的自定义对象,你必须先序列化它。更好的解决方案可能是将您的对象转换为 plist 字典(每个属性都成为字典中的键值)。

于 2016-05-16T06:27:58.443 回答