In general, iOS has a partial page load process, creating a model then associating the nib file with the model and then quickly getting to the control instance on the nib file. The Action Generation page. But the original content is not directly through the JSON get model can only generate a dictionary. Then convert to model. The following method is the process of converting a dictionary to model. Convert a 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 = [[NSString alloc] initWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
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 code other two helper type methods copy 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 code to convert model to 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, nor is it nil
[dic setObject: propertyValue forKey: key];
}
return dic;
}
"Objective-c" dictionary quickly converted to model code