IOS dictionary quick conversion to Model
In general, the process for IOS to load partial pages is to create a Model, associate the Nib file with the Model, and then quickly obtain the control instance on the Nib file. Operation generation page. However, the native content does not directly obtain the Model through Json and can only generate dictionaries. Then convert it to Model. The following methods are used to convert data into a Model through dictionaries. Convert the dictionary to Model copy code-(BOOL) reflectDataFromOtherObject :( NSDictionary *) dic {unsigned int outCount, I; objc_property_t * properties = class_copyPropertyList ([self class], & outCount ); for (I = 0; I <outCount; I ++) {objc_property_t property = properties [I]; NSString * propertyName = [[NSString alloc] initWithCString: property_getName (property) encoding: NSUTF8StringEncoding]; NSString * propertyType = [NSSt Ring alloc] initWithCString: property_getAttributes (property) encoding: encoding]; if ([dic allKeys] containsObject: propertyName]) {id value = [dic valueForKey: propertyName]; if (! [Value isKindOfClass: [NSNull class] & value! = Nil) {if ([value isKindOfClass: [NSDictionary class]) {id pro = [self createInstanceByClassName: [self getClassName: propertyType]; [pro reflectDataFromOtherObject: value]; [self setValue: pro forKey: propertyName];} else {[self setValue: value forKey: propertyName] ;}}} free (properties); return true ;} copy the code to the other two auxiliary types. Copy the code-(NSString *) getClassName :( NSString *) attributes {NSString * type = [attributes substringFromIndex: [attributes rangeOfRegex: @ "\" "]. location + 1]; type = [type substringToIndex: [type rangeOfRegex: @ "\" "]. location]; return type;}-(id) createInstanceByClassName: (NSString *) className {NSBundle * bundle = [NSBundle mainBundle]; Class aClass = [bundle classNamed: className]; id anInstance = [[aClass alloc] init]; return anInstance;} copy the code to convert the Model into a dictionary copy code-(NSDictionary *) convertModelToDictionary {NSMutableDictionary * dic = [[NSMutableDictionary alloc] init]; for (NSString * key in [self propertyKeys]) {id propertyValue = [self valueForKey: key]; // The value is not NSNULL and is not nil [dic setObject: propertyValue forKey: key];} return dic ;}