IOS dictionary to Model quickly, ios dictionary to model

Source: Internet
Author: User

IOS dictionary to Model quickly, ios dictionary 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 a dictionary to a Model

-(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;}

Other two auxiliary methods

-(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;}

 

Convert Model to Dictionary

-(NSDictionary *) convertModelToDictionary {NSMutableDictionary * dic = [[NSMutableDictionary alloc] init]; for (NSString * key in [self propertyKeys]) {id propertyValue = [self valueForKey: key]; // The value is neither NSNULL nor nil [dic setObject: propertyValue forKey: key];} return dic ;}

 


How to convert a string into a dictionary in ios development

What kind of string is converted into a dictionary? Is it in json format? If yes, you can convert it.
 
How can I change the following code of converting a dictionary to a string?

Do you want to convert the dictionary to string storage, or just output the above format?
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.