IOS development diary 18-runtime advanced, ios diary 18-runtime

Source: Internet
Author: User

IOS development diary 18-runtime advanced, ios diary 18-runtime

Today, the blogger has an advanced runtime requirement and encountered some difficulties. I would like to share with you the hope to make common progress.

I believe that after reading the previous blog post of the blogger, you have a certain understanding of the runtime. When the interviewer asks you what the runtime is, I believe that you will not just say that there is no word at runtime. after you finish your understanding of runtime, most interviewers will ask you, have you used runtime?

Runtime is a dangerous technology. We recommend that you do not use runtime easily in Apple's official documents. However, we should still master the most basic usage. There are many runtime functions. We can click the header file.

View Details in objc/runtime. h

 

Related functions

 

1. Add

 

Add function: class_addMethod

 

Add instance variable: class_addIvar

 

Add attribute: @ dynamic label or class_addMethod, because the attribute is actually composed of the getter and setter functions.

 

Add Protocol: class_addProtocol (to be honest, I really don't know how to add a protocol dynamically ,-_-!!)

 

2. Get

 

Obtain the function list and information of each function (function pointer, function name, etc.): class_getClassMethod method_getName...

 

Obtain the attribute list and information of each attribute: class_copyPropertyList property_getName

 

Obtain information about the class, such as the class name: class_getName class_getInstanceSize

 

Get the Variable list and variable information: class_copyIvarList

 

Get the variable value

 

3. Replace

 

Replace the instance with another class: object_setClass

 

Definition of the replacement class method: class_replaceMethod

 

4. Other common methods:

 

Implement the exchange of two methods: method_exchangeImplementations.

 

Set the implementation of a method: method_setImplementation.

 

The simplest runtime application needs to use runtime to view the class attribute list, method List, Member Variable list, and protocol list. The code below will be posted to share with you.

Unsigned int count;

// Obtain the attribute list

Objc_property_t * propertyList = class_copyPropertyList ([self class], & count );

For (unsigned int I = 0; I <count; I ++ ){

Const char * propertyname = property_getName (propertyList [I]);

NSLog (@ "property ----- % @", [NSString stringwithuf8string: propertyname]);

}

// Obtain the method list

Method * methodList = class_copyMethodList ([self class], & count );

For (unsigned int I; I <count; I ++ ){

Method method = methodList [I];

NSLog (@ "method ----- % @", NSStringFromSelector (method_getName (method )));

}

// Obtain the member variable list

Ivar * ivarList = class_copyIvarList ([self class], & count );

For (unsigned int I; I <count; I ++ ){

Ivar myIvar = ivarList [I];

Const char * ivarName = ivar_getName (myIvar );

NSLog (@ "ivar ------ % @", [NSString stringwithuf8string: ivarName]);

}

// Obtain the protocol list

_ Unsafe_unretained Protocol ** protocolList = class_copyProtocolList ([self class], & count );

For (unsigned int I; I <count; I ++ ){

Protocol * myProtocol = protocolList [I];

Const char * protocolName = protocol_getName (myProtocol );

NSLog (@ "protocol ------ % @", [NSString stringwithuf8string: protocolName]);

}

Of course, there are some more complex applications that will not be elaborated here. The following blogs will help you better understand runtime.

Http://www.cocoachina.com/ios/20150901/13173.html? Utm_source = tuicool

Http://www.cocoachina.com/ios/20150907/13336.html? Utm_source = tuicool

Http://hechen.info/2015/09/07/Understanding-the-Objective-C-Runtime? Utm_source = tuicool

Http://www.tuicool.com/articles/MvM3ie? Plg_nld = 1 & plg_uin = 1 & plg_auth = 1 & plg_nld = 1 & plg_usr = 1 & plg_vkey = 1 & plg_dev = 1

Http://www.tuicool.com/articles/uyaAZjM? Plg_nld = 1 & plg_uin = 1 & plg_auth = 1 & plg_nld = 1 & plg_usr = 1 & plg_vkey = 1 & plg_dev = 1

Http://www.cocoachina.com/ios/20150824/13104.html? Utm_source = tuicool

Related Article

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.