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