iOS Dictionary goto model

Source: Internet
Author: User

iOS dictionary to model label:ios  dictionary to model      One, the custom method in the model class to implement, note: The property name and the dictionary key to the actual data is the same as the properties of a, Implementation in model class 123456789101112131415161718192021222324252627282930    //  model class  .h file          @interface  Person: NSObject         @property   (nonatomic,copy)  NSString *name;    @property   (nonatomic, Assign)  UIInteger age;       //  Customize this method          -  (Instancetype) Initwithdict: (nsdictionary *) dict;      +  (Instancetype) Personwithdict: (nsdictionary *) dict;           @end      //  Model class  .M file implementation    -  (instancetype) Initwithdict: (nsdictionary *) dict {        if   (Self = [super init]) {                            self.name = dict[@ "Name"];               self.age = dict[@ "Age"];           }                    return self; }  +  (instancetype) Personwithdict: (nsdictionary *) dict {         return [ [self alloc] initwithdict:dict];  }     B, In the Get model data class, implement 12     PERSON *P = [PERSON ALLOC] INITWITHDICT: Dict]; (Here Direct dictionary to model)  // Person *p = [Person personWithDict:dict];      Second, directly with KVC implementation, note that the model property to be the same as the actual property of the data, otherwiseUsing the KVC method will error A, implementation in the model class 1234567891011121314151617181920212223242526272829303132    //  model class  .h file         @interface  Person: NSObject         @property   (nonatomic,copy)  NSString *name;    @property   (nonatomic,assign)  UIInteger age;       //  Customizing this method     -  (Instancetype) Initwithdict: (nsdictionary *) dict; +  (instancetype ) Personwithdict: (nsdictionary *) dict;      @end     //  Model class  .M File implementation    -  (Instancetype) Initwithdict: (nsdictionary *) dict {         if  (Self = [super init]) {                            // self.name = dict[@ "Name"];             //  self.age = dict[@ "Age"];                              [self setValuesForKeysWithDictionary:dict];   (implemented here)      }                     return self; }  +  (Instancetype) Personwithdict: (nsdictionary *) dict {         return [ [self alloc] initWithDict:dict];   }    B implementation in the Get model data class 12    1  person *p =  [Person alloc] initWithDict:dict]; (here Direct dictionary to model) 2  // person *p = [ Person personwithdict:dict];     third-party framework to implement the specific implementation of: mjextension  , such as the introduction of GitHub 


This article is from the "Technical Blog" blog, please be sure to keep this source http://9077272.blog.51cto.com/9067272/1633794

iOS Dictionary goto model

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.