The first time you see runtime, feel too big on, dynamic access to methods, attributes, and so on is really bad. After looking for data + practice, found that the runtime is not as complicated as imagined, then the runtime is a basic introduction.
To use the run-time method, you need to introduce the Runtime.h file
First, the basic knowledge
Method: Member Methods
Ivar: Member Variable
Second, common methods
Class_copypropertylist: Get Property List
Class_copymethodlist: Get a list of member methods
Class_copyivarlist: Get a list of member variables
Ivar_getname: Get variable name
Property_getname: Get Property name
Examples of Use:
1. Get a list of member variables
//1. Get the variable listUnsignedintIvarcount =0;//number of member variablesIvar *ivarlist = Class_copyivarlist ([selfclass], &ivarcount);//Ivar Array for(inti =0; i < Ivarcount; i++) {//TraverseIvar Ivar = Ivarlist[i];//Get Ivar Const Char*name = Ivar_getname (Ivar);//Get variable nameNSString *key =[NSString Stringwithutf8string:name]; NSLog (@"%@", key); }
Free (ivarlist);
2. Get a list of properties
UnsignedintCount =0; objc_property_t*propertlist = Class_copypropertylist ([selfclass], &count); for(inti =0; I < count; i++) {objc_property_t property=Propertlist[i]; Const Char*name =Property_getname (property); Const Char*attrs =Property_getattributes (property);//Property_copyattributevalue (,) the first parameter is objc_property_t, the second parameter "V" gets the variable name, "T" gets the type Const Char*value = Property_copyattributevalue (property,"V"); NSLog (@"name =%s, Attrs =%s, value =%s", name, attrs, value); }
Free (propertlist);
3. Get a list of methods
UnsignedintCount =0; Method*methodlist = Class_copymethodlist ([selfclass], &count); for(inti =0; I < count; i++) {Method method=Methodlist[i]; SEL selector= Method_getname (method);//Method Entry Const Char*sel_name =Sel_getname (selector); NSLog (@"Method Name%s", Sel_name); } free (methodlist);
Third, use direction: archive, Dictionary <----> model, frame package, etc.
Implementing archiving
#defineWkcodingimplementing-(void) Encodewithcoder: (Nscoder *) Acoder {unsignedintIvarcount =0; Ivar*ivarlist = Class_copyivarlist ([selfclass], &ivarcount); for(inti =0; i < Ivarcount; i++) {Ivar Ivar=Ivarlist[i]; Const Char*name =Ivar_getname (Ivar); Const Char*type =ivar_gettypeencoding (Ivar); NSLog (@"%s-----%s", name, type); NSString*key =[NSString Stringwithutf8string:name]; IDValue =[self valueforkey:key]; [Acoder Encodeobject:value Forkey:key]; } free (ivarlist); } -(Nullable Instancetype) Initwithcoder: (Nscoder *) Adecoder {if(self =[Super Init]) {unsignedintIvarcount =0; Ivar*ivarlist = Class_copyivarlist ([selfclass], &ivarcount); for(inti =0; i < Ivarcount; i++) {Ivar Ivar=Ivarlist[i]; Const Char*name =Ivar_getname (Ivar); NSString*key =[NSString Stringwithutf8string:name]; NSLog (@"%@ %@", key, value); IDValue =[Adecoder Decodeobjectforkey:key]; [Self setvalue:value forkey:key]; } } returnSelf ;}
iOS Advanced: objective-c runtime (i)