In today's iOS development, we typically use the MVC pattern. When we get the data, we have to turn the data into a model to use.
In general, all the data we get are dictionaries. This is what we need to turn the dictionary into a model object.
When the properties of the object are very few, we can write the key value of the dictionary directly and convert it.
_ Property = dict["Key"]
When the properties of the object are many, we can use the KVC batch setup.
setvaluesforkeyswithdictionary:<# (nsdictionary *) #>
However, when the KVC batch turn, there is a fatal disadvantage, that is, when the dictionary key, in the object properties can not find the corresponding property in the time
Will error.
At this time, we might as well think about it in turn. We first get all the property names of the objects, then add them to an array, and then iterate over and assign the values.
When the program is running, grab the properties of the object, this time, to use the runtime mechanism, details see the following code
1+ (Nsarray *) propertylist {2 3 //0. Determine if there is an associated object, and if so, return directly4/**5 the object to which the 1> is associated6 2> properties associated with key7 8 tip: In OC, a class is essentially an object9 */TenNsarray *plist =Objc_getassociatedobject (self, propertieskey); Oneif(PList! =Nil) { AreturnpList; - } - the //1. Get the properties of ' class ' -/** - Parameters - 1> class + The Count pointer for the 2> property - */ +UnsignedintCount =0; A//The return value is an array of all properties objc_property_t atobjc_property_t *list = Class_copypropertylist ([selfclass], &count); - -Nsmutablearray *arraym =[Nsmutablearray Arraywithcapacity:count]; - -//iterating through an array - for(unsignedinti =0; I < count; ++i) { in//Get to Property -objc_property_t pty =List[i]; to +//gets the name of the property -Const Char*cname =Property_getname (pty); the * [Arraym addobject:[nsstring stringwithutf8string:cname]; $ }Panax NotoginsengNSLog (@"%@", Arraym); - the//releasing an array of attributes + Free (list); A the//Setting Association Objects +/** - 1> objects associated with $ 2> Key of the associated object $ 3> Property Value - 4> Properties held reatin, copy, assign - */ the Objc_setassociatedobject (self, propertieskey, Arraym, objc_association_copy_nonatomic); - Wuyireturnarraym.copy; the}
In the above code, there are two paragraphs that determine if there is an associated object, and the code that sets the associated object. is to not execute this method repeatedly.
After getting to the above attribute array, you can write a dictionary to the model method.
1+ (Instancetype) objectwithdict: (Nsdictionary *) Dict {2IDobj =[[Self alloc] init];3 4//[obj setvaluesforkeyswithdictionary:dict];5Nsarray *properties =[self propertylist];6 7//iterating through an array of properties8 for(NSString *keyinchproperties) {9//determine if this key is included in the dictionaryTenif(Dict[key]! =Nil) { One//setting values using KVC A [obj Setvalue:dict[key] forkeypath:key]; - } - } the - - returnobj; -}
iOS Development-use runtime (runtime) dictionary to model