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