iOS Dictionary goto model

Source: Internet
Author: User

 first, in the model class to implement the custom method , Note: The property name and the dictionary key to be the same as the actual data properties

A, implementation in the model class

  Model class  .h files        @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. Implementation in the Get model data class

Person *p = [Person alloc] initwithdict:dict]; (Here Direct dictionary to model)//person *p = [person personwithdict:dict];


Second, directly with the KVC implementation, pay attention to the model properties and data the actual properties of the same, otherwise the KVC method will be error

  A, the implementation in the Model class

  Model class  .h files        @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 = d ict[@ "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

1 person *p = [Person alloc] initwithdict:dict]; (Here Direct dictionary to model) 2/person *p = [person personwithdict:dict];


Third, the use of a well-packaged three-party framework to achieve

such as: Mjextension specific implementation to see the introduction of GitHub

This article is from the "Future" blog, please be sure to keep this source http://yuanshui.blog.51cto.com/2097536/1632843

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.