1. 程式人生 > >2012-6 筆記--動態獲取屬性名,構造json結構

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 [email protected]"NSImageLoading",&,N,Vm_imagel99999

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

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

2012-06-21 10:30:15.192 contacts[30222:9203] m_strName [email protected]"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"]}