iOS development trick-adding properties to a class using the runtime runtime method

Source: Internet
Author: User

Read some of the source code of the third-party open source class library, often found that they have added a non-existent property to a class,

For example, Pprealsidecontroller adds a Self.pprealsidecontroller attribute to Uiviewcontroller?

How did he achieve that?


1. Basic idea of realization


The first thing we need to know is that when we use a self.newproperty-like statement,

According to the rules of point syntax is actually called the Setnewproperty method, and the NewProperty method,

Then we know that he must have added a class to a uiviewcontroller, and then added a

The set and Get Methods (IBID.), so we can see here that the self.newproperty he added actually

Just a formal attribute, when you use the _newproperry is not possible!

So this form of ' attribute ' can only be invoked using the form of a point.


2. Use the runtime method to establish an association


Although we know that you can use a class to add two methods to a class, that is, an indirect property,

But how do we correlate the value of the property we need to store with it?

In the <objc/runtime.h> runtime framework provides us with a corresponding method that allows us to use a static key

Assigning a value to a piece of memory, you can also use this key to get the corresponding value


The corresponding methods are:

    Objc_setassociatedobject (self, &staticmanagerkey,                             Manager,                             Objc_association_retain);


And

Objc_getassociatedobject (self, &staticmanagerkey);


Examples of Use:

We add a manager property to a Uiviewcontroller class:

We need to create a Uiviewcontroller class with detailed code as follows:

. h

  uiviewcontroller+bbmanager.h//  huanyouwang////  Created by Mbinyang on 15/4/23.//  Copyright (c) 2015 Cc.huanyouwang. All rights reserved.//#import <UIKit/UIKit.h> #import "BBRequest.h" @interface Uiviewcontroller () @property ( strong,nonatomic) bbrequest *manager, @end @interface uiviewcontroller (Bbmanager) @end

You can see that we have added a manager property to Uiviewcontroller, which is actually the function of providing us with a Setmanager method in. h, and the manager method;


. m

  uiviewcontroller+bbmanager.m//  huanyouwang////  Created by Mbinyang on 15/4/23.//  Copyright (c) 2015 Cc.huanyouwang. All rights reserved.//#import "Uiviewcontroller+bbmanager.h" #import <objc/runtime.h> @implementation Uiviewcontroller (bbmanager) static char staticmanagerkey;-(void) Setmanager: (Bbrequest *) manager{    [self willchangevalueforkey:@ "Staticmanagerkey"];    Objc_setassociatedobject (self, &staticmanagerkey,                             Manager,                             Objc_association_retain);    [Self didchangevalueforkey:@ "Staticmanagerkey"];} -(Bbrequest *) manager{    return Objc_getassociatedobject (self, &staticmanagerkey);} @end

As you can see, we implemented the Set,get method in. M.


Using the example, we test in a Uiviewcontroller subclass:

Import in Testviewcontroller

Uiviewcontroller+bbmanager.h

Then test:

Self.manager = [[Bbrequest alloc]init];//Call set method    [Self.manager dosomething];//call  Get method

You can see that we have added a property manager for a Uiviewcontroller success.


More Original: Http://blog.csdn.net/yangbingbinga


Next: Use the runtime to highly wrap the AF and HUD


Summary: When we use the run-time method to add properties to a class, we can use it very conveniently.







iOS development trick-adding properties to a class using the runtime runtime method

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.