IOS dictionary quickly converted to model

Source: Internet
Author: User

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

-(BOOL) Reflectdatafromotherobject: (Nsdictionary *) dic{unsignedintOutcount, I; objc_property_t*properties = Class_copypropertylist ([selfclass], &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]) {IDValue =[dic Valueforkey:propertyname]; if(! [Value Iskindofclass:[nsnullclass]] && Value! =Nil) {                if([Value Iskindofclass:[nsdictionaryclass]]) {                    IDPro =[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 helper type methods

-(NSString *) GetClassName: (NSString *) attributes{NSString*type = [Attributes Substringfromindex:[attributes Rangeofregex:@"\ ""].location + 1]; Type = [Type Substringtoindex:[type rangeofregex:@"\""].location]; returntype;}-(ID) Createinstancebyclassname: (NSString *) className {nsbundle*bundle =[NSBundle Mainbundle]; Class AClass=[bundle Classnamed:classname]; IDAninstance =[[AClass alloc] init]; returnaninstance;}

Convert model to Dictionary

-(Nsdictionary *) convertmodeltodictionary{    *dic = [[Nsmutabledictionary alloc] init];          for inch [self PropertyKeys]) {        id propertyvalue = [self valueforkey:key];         // The value is not nsnull and is not nil         [dic setobject:propertyvalue Forkey:key];    }         return DiC;}

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.