/** if you want to dynamically get the properties of a class, you need to use the Class_copyivarlist member variable Class_copypropertylist property Class_copymethodlist method to the runtime mechanism Class_ Copyprotocollist protocol *////Returns the property list of the Class + (Nsarray *) propertylist {//0. Determine if there is an associated object, and if so, directly return the/** parameter 1> the associated property k associated with the object 2> EY */nsarray *plist = Objc_getassociatedobject (self, Propertieskey), if (plist) {return plist;} 1. Gets the ' class's Properties '/** 1> class 2> property's count pointer */unsigned int count = 0;//return value is an array of all properties obj_property_objc_property_t *list = class_copy PropertyList ([Self class], &count);//Create Storage property array Nsmutablearray *arraym = [Nsmutablearray arraywithcapacity:count]; for (int i = 0; i < count; ++i) {//Get property objc_property_t pty = list[i];//Get property name const char *cname = Property_getname (pty) ; [Arraym addobject:[nsstring stringwithutf8string:cname];} Releases the property array free (list);//sets the associated object/** 1> The associated object 2> the key 3> property of the associated object 4> the holding method of the attribute Reatin, copy, assign */objc_setassoc Iatedobject (self, propertieskey, Arraym, objc_association_copy_nonatomic); return arraym.copy;}
Dictionary-to-model method
Dictionary to model Method + (Instancetype) objectwithdict: (nsdictionary *) Dict{id obj = [[Self alloc] init];//get property list Nsarray *properties = [Self propertylist];//traversal property array for (NSString *key in properties) {//Determine if the dictionary contains this keyif (Dict[key]! = nil) {//Use KVC to set property values [ obj Setvalue:dict[key] forkeypath:key];}} return obj;}
2. Returning a list of methods for a class