In iOS development, the dictionary-to-model is generally as follows:
1 /**2 * Declaration Method3 */4-(Instancetype) Initwithdictionary: (Nsdictionary *) dict;5+ (Instancetype) carwithdictionary: (Nsdictionary *) dict;6 7 /**8 * Implementation Method9 */Ten-(Instancetype) Initwithdictionary: (Nsdictionary *) Dict One { A if(self =[Super Init]) { - [self setvaluesforkeyswithdictionary:dict]; - } the returnSelf ; - } - -+ (Instancetype) carwithdictionary: (Nsdictionary *) Dict + { - return[[Zycar alloc]initwithdictionary:dict]; +}
Declared in the header file before the. m
File to implement specific methods, and the content of these methods are basically consistent, can be simplified with macros.
2. Create a new. h file and add the following code to it:
1 #defineInith (name)2-(Instancetype) Initwithdict: (Nsdictionary *) Dict; 3+ (instancetype) name# #WithDict:(nsdictionary *) dict;4 5 #defineINITM (name)6-(Instancetype) Initwithdict: (Nsdictionary *) Dict7 { 8 if(self =[Super Init]) { 9 [self setvaluesforkeyswithdictionary:dict];Ten } One returnSelf ; A } -+ (instancetype) name# #WithDict:(nsdictionary *) Dict - { the return[[Self alloc] initwithdict:dict]; - } - - #endif
After quoting this. h file, the Declaration and implementation of the above method can be reduced to a sentence
Inith (name); Initm (name); Name is the model class name