iOS Advanced: objective-c runtime (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.