Converting dictionaries into models for model development is the most common feature in development. KVC can be used to convert a dictionary into a model, but there are three constraints. One is to ensure that the number of attributes of the model is greater than or equal to the number of dictionaries. The other is that the attribute name must be the same as the dictionary key, third, the basic data types in the model cannot be converted.
The first and third are their biggest drawbacks. For example, if you obtain a data dictionary from the server, you do not want to use all key-value pairs. However, in development, you must list all the corresponding attributes and convert them (unless you write them one by one without using KVC ).
The following code solves the above problem: the model-based conversion is implemented to partially convert Dictionary data and identify basic data types. The constraint only requires that the model attribute name be consistent with the dictionary KEY (because the underlying layer still uses KVC );
1 // 2 // NSObject + Value. m 3 // 4 // Created by shunzi 5-10-2014. 5 // Copyright (c) 2014 shunzi. all rights reserved. 6 // 7 8 # import "NSObject + Value. h "9 # import <objc/message. h> 10 # import <objc/runtime. h> 11 12 @ implementation NSObject (Value) 13-(void) setValues :( NSDictionary *) values14 {15 Class c = [self class]; 16 17 while (c) {18 // 1. get all member variables 19 unsigned int outCount = 0; 20 Ivar * ivars = c Lass_copyIvarList (c, & outCount); 21 22 for (int I = 0; I <outCount; I ++) {23 Ivar ivar = ivars [I]; 24 25 // 2. attribute name 26 NSMutableString * name = [NSMutableString stringwithuf8string: ivar_getName (ivar)]; 27 28 // Delete the frontend _ 29 [name replaceCharactersInRange: NSMakeRange (0, 1) withString: @ ""]; 30 31 // 3. retrieve the attribute value 32 NSString * key = name; 33 if ([key isEqualToString: @ "desc"]) {34 key = @ "description"; 35} 36 if ([key is Required tostring: @ "ID"]) {37 key = @ "id"; 38} 39 id value = values [key]; 40 if (! Value) continue; 41 42 // 4. SEL43 // initial 44 NSString * cap = [name substringToIndex: 1]; 45 // change to uppercase 46 cap = cap. uppercaseString; 47 // Replace the upper letter with the first letter 48 [name replaceCharactersInRange: NSMakeRange (0, 1) withString: cap]; 49 // splice set50 [name insertString: @ "set" atIndex: 0]; 51 // concatenate a colon: 52 [name appendString: @ ":"]; 53 SEL selector = NSSelectorFromString (name ); 54 55 // 5. attribute type 56 NSString * type = [NSString stringwithuf8string: ivar_getTypeEncoding (ivar)]; 57 58 if ([type hasPrefix: @ "@"]) {// object type 59 objc_msgSend (self, selector, value); 60} else {// non-object type 61 if ([type is1_tostring: @ "d"]) {62 objc_msgSend (self, selector, [value doubleValue]); 63} else if ([type isEqualToString: @ "f"]) {64 objc_msgSend (self, selector, [value floatValue]); 65} else if ([type isEqualToString: @ "I"]) {66 objc_msgSend (self, selector, [value intValue]); 67} else {68 objc_msgSend (self, selector, [value longLongValue]); 69} 70} 71} 72 73 c = class_getSuperclass (c); 74} 75} 76 @ end
The header file only contains the declaration of this method, so it is not pasted here