(nsdictionary*)getobjectdata:(id)obj
{
nsmutabledictionary *dic = [nsmutabledictionary dictionary];
unsigned int propscount;
objc_property_t *props = class_copypropertylist([obj class], &propscount);
for(int i = 0;i < propscount; i ) {
objc_property_t prop = props[i];
id value = nil;
@try {
nsstring *propname = [nsstring stringwithutf8string:property_getname(prop)];
value = [self getobjectinternal:[obj valueforkey:propname]];
if(value != nil) {
[dic setobject:value forkey:propname];
}
}
@catch (nsexception *exception) {
[self logerror:exception];
}
}
return dic;
}
(void)print:(id)obj
{
nslog(@"%@", [self getobjectdata:obj]);
}
(nsdata*)getjson:(id)obj options:(nsjsonwritingoptions)options error:(nserror**)error
{
return [nsjsonserialization datawithjsonobject:[self getobjectdata:obj] options:options error:error];
}
(id)getobjectinternal:(id)obj
{
if(!obj
|| [obj iskindofclass:[nsstring class]]
|| [obj iskindofclass:[nsnumber class]]
|| [obj iskindofclass:[nsnull class]]) {
return obj;
}
if([obj iskindofclass:[nsarray class]]) {
nsarray *objarr = obj;
nsmutablearray *arr = [nsmutablearray arraywithcapacity:objarr.count];
for(int i = 0;i < objarr.count; i ) {
[arr setobject:[self getobjectinternal:[objarr objectatindex:i]] atindexedsubscript:i];
}
return arr;
}
if([obj iskindofclass:[nsdictionary class]]) {
nsdictionary *objdic = obj;
nsmutabledictionary *dic = [nsmutabledictionary dictionarywithcapacity:[objdic count]];
for(nsstring *key in objdic.allkeys) {
[dic setobject:[self getobjectinternal:[objdic objectforkey:key]] forkey:key];
}
return dic;
}
return [self getobjectdata:obj];
}
(void)logerror:(nsexception*)exp
{
#if print_obj_logging
nslog(@"printobject error: %@", exp);
#endif
}
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
我是段花花花花花花