Creating an object that works with NSJSONSerialization dataWithJSONObject:options:error:

余生长醉 提交于 2019-12-23 12:07:00

问题


At the moment in order to use this function I'm "converting" my object into a dictionary. i.e. the property names become the keys and the property values become the values.

Is there a way to properly do this so that an object will work with this function?

i.e. similar to encodeWithCoder and initWithCoder for use with NSUserDefaults.

I'd like to do ...

NSData *data = [NSJSONSerialization dataWithJSONObject:myObject options:0 error:nil];

at the moment I do something like...

NSData *data = [NSJSONSerialization dataWithJSONObject:[myObject dictionaryRepresentation] options:0 error:nil];

回答1:


Take a look at JSONCoding, which builds on NSJSONSerialization with an NSCoding interface.



来源:https://stackoverflow.com/questions/14817881/creating-an-object-that-works-with-nsjsonserialization-datawithjsonobjectoption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!