2012-6 笔记--动态获取属性名,构造json结构


沙盒中图片,文件如果没有,可以重新加载图片来解决






2012-06-21 10:30:15.189 contacts[30222:9203] m_intv Ti,N,Vm_intv

2012-06-21 10:30:15.190 contacts[30222:9203] m_imagel99999 T@"NSImageLoading",&,N,Vm_imagel99999

2012-06-21 10:30:15.190 contacts[30222:9203] m_strIcon T@"NSString",&,N,Vm_strIcon

2012-06-21 10:30:15.191 contacts[30222:9203] m_strInfo T@"NSString",&,N,Vm_strInfo

2012-06-21 10:30:15.192 contacts[30222:9203] m_strName T@"NSString",&,N,Vm_strName


---

NSMutableArray * testArray = [[NSMutableArrayalloc]init];

NSString * tt =nil;

[testArrayaddObject:@"1"];

[testArrayaddObject:nil];

[testArrayaddObject:tt];

[testArrayaddObject:@"2"];


unable to read unknown load command 0x26

Current language:  auto; currently objective-c

2012-06-21 15:07:31.427 contacts[35851:9203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 1'

*** Call stack at first throw:

(

0   CoreFoundation                      0x00db9be9 __exceptionPreprocess + 185

1   libobjc.A.dylib                     0x00f0e5c2 objc_exception_throw + 47

2   CoreFoundation                      0x00db37f1 -[__NSArrayM insertObject:atIndex:] + 225

3   CoreFoundation                      0x00daec44 -[__NSArrayM addObject:] + 68


不能向数组中插入nil


根据结构进行递归封装到字典中

+ (NSMutableDictionary *) getDicFromObject: (id) theObject

{

NSMutableDictionary * tmpDic = [[NSMutableDictionaryalloc]init];

if (theObject ==nil)

{

return tmpDic;

}

//NSString * theObject = [[PersonInfo alloc] init];

NSString *className =NSStringFromClass([theObjectclass]);

    constchar *cClassName = [className UTF8String];

    id theClass =objc_getClass(cClassName);

unsignedint outCount, i;

    objc_property_t *properties =class_copyPropertyList(theClass, &outCount);

    NSMutableArray *propertyNames = [[NSMutableArrayalloc]initWithCapacity:1];

    for (i =0; i < outCount; i++) 

{

        objc_property_t property = properties[i];

       NSString *propertyNameString = [[NSStringalloc]initWithCString:property_getName(property)encoding:NSUTF8StringEncoding]; 

        [propertyNamesaddObject:propertyNameString];

        [propertyNameStringrelease];

       NSLog(@"%s %s\n",property_getName(property),property_getAttributes(property));

    }

for(NSString *keyin propertyNames)

    {

        SEL selector =NSSelectorFromString(key);

        id value = [theObjectperformSelector:selector];

        if (value ==nil)

        {

            value = [NSNullnull];

[tmpDicsetObject:valueforKey:key];

continue;

        }

       //[finalDict setObject:value forKey:key];

if ([valueisKindOfClass:[NSStringclass]])

{

[tmpDicsetObject:valueforKey:key];

}

elseif ([valueisKindOfClass:[NSArrayclass]])

{

NSArray * aArray = (NSArray *)value;

for (int i =0; i < [aArraycount]; i++)

{

[tmpDicsetObject:[NetManagergetDicFromObject:[aArrayobjectAtIndex:i]]

 forKey:key];

}

}

else 

{

[tmpDicsetObject:[NetManagergetDicFromObject:value]forKey:key];

}

    }

return tmpDic;

}




采用递归的方法访问一个对象,讲其内容写到一个字典中

支持的成员变量类型:

1,NSString *

2,NSArray *

3,由1,2,构成的自定义类

+ (NSMutableDictionary *) getDicFromObject: (id) theObject

{

if ([theObjectisKindOfClass:[NSStringclass]])

{

return theObject;

}

NSMutableDictionary * tmpDic = [[[NSMutableDictionaryalloc]init] autorelease];

if (theObject ==nil)

{

return tmpDic;

}

//get all the property fields names

NSString *className =NSStringFromClass([theObjectclass]);

    constchar *cClassName = [className UTF8String];

    id theClass =objc_getClass(cClassName);

unsignedint outCount, i;

    objc_property_t *properties =class_copyPropertyList(theClass, &outCount);

    NSMutableArray *propertyNames = [[NSMutableArrayalloc]initWithCapacity:1];

    for (i =0; i < outCount; i++) 

{

        objc_property_t property = properties[i];

        NSString *propertyNameString = [[NSStringalloc]initWithCString:property_getName(property) 

encoding:NSUTF8StringEncoding]; 

        [propertyNamesaddObject:propertyNameString];

        [propertyNameStringrelease];

       NSLog(@"%s %s\n",property_getName(property),property_getAttributes(property));

    }

for (NSString *keyin propertyNames)

    {

        SEL selector =NSSelectorFromString(key);

        id value = [theObjectperformSelector:selector];

        if (value ==nil)

        {

            value = [NSNullnull];

[tmpDicsetObject:valueforKey:key];

continue;

        }

       //[finalDict setObject:value forKey:key];

if ([valueisKindOfClass:[NSStringclass]])

{

[tmpDicsetObject:valueforKey:key];

}

elseif ([valueisKindOfClass:[NSArrayclass]])

{

NSMutableArray * valueArray = [[[NSMutableArrayalloc]init]autorelease];

NSArray * aArray = (NSArray *)value;

for (int i =0; i < [aArraycount]; i++)

{

[valueArrayaddObject:[NetRequestManagergetDicFromObject:[aArrayobjectAtIndex:i]]];

//[tmpDic setObject:[NetRequestManager getDicFromObject:[aArray objectAtIndex:i]]

//  forKey:key];

}

[tmpDicsetObject:valueArrayforKey:key];

}

else 

{

[tmpDicsetObject:[NetRequestManagergetDicFromObject:value]forKey:key];

}

    }

return tmpDic;

}








ContactBaseInfoObj * obj = [[ContactBaseInfoObjalloc]init];

obj.name = [[[NameObjalloc]init]autorelease];

obj.name.firstName =@"Jam";

obj.name.middleName =@"Alen";

obj.name.lastName =@"Green";

obj.primaryEmail =@"America";

NSMutableDictionary * tmpDic = [NetRequestManagergetDicFromObject:obj];

NSString * tmpStr = [tmpDicJSONRepresentation];

NSLog(tmpStr);

NSArray * ids = [NSArrayarrayWithObjects:@"mm",@"Mary",@"yy",nil];

ContactBaseWithBothIDObj * tmpCC = [[ContactBaseWithBothIDObjalloc]init];

tmpCC.contactBaseInfo = obj;

tmpCC.contactID =@"123456";

tmpCC.contactListIDs = ids;

tmpDic = [NetRequestManagergetDicFromObject:tmpCC];

tmpStr = [tmpDicJSONRepresentation];

NSLog(tmpStr);

NSString * testStr = [NetRequestManager getCodeFromObject:obj];

testStr = [NetRequestManager getCodeFromObject:tmpCC];




{"primaryEmail":"America","primaryMobile":null,"name":null}


{"primaryEmail":"America","primaryMobile":null,"name":{"firstName":"Jam","middleName":"Alen","lastName":"Green"}}



{"contactID":"123456","contactBaseInfo":{"primaryEmail":"America","primaryMobile":null,"name":{"firstName":"Jam","middleName":"Alen","lastName":"Green"}},"contactListIDs":["mm","Mary","yy"]}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值