Think of how you can add instance variables to all of your objects? We know that using category makes it easy to add methods to existing classes, but it is not possible to increase the instance variables directly. However, starting with Mac OS X v10.6, the system provides associative References, which is an easy problem to solve. This method is called Association (Association), we can dynamically add any number of properties during runtime, and read at any time. The two important runtime APIs used are:
const void *key, id value, Objc_associationpolicy policy)
Objc_export ID objc_getassociatedobject (ID object, const void *key) __osx_available_starting (__mac_10_6, __iphone_3_1) ;
Now let's combine a practical example to illustrate their usage. Let's say we're now going to use category to add properties to the Uilabel, so we can do this:
#import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UILabel (Associate)-(void) Setflashcolor: ( Uicolor *) flashcolor;-(Uicolor *) Getflashcolor; @end
#import "Uilabel+associate.h" @implementation UILabel (Associate)static char Flashcolorkey; -(void) Setflashcolor: (Uicolor *) flashcolor{ objc_association_retain_nonatomic);} -(Uicolor *) getflashcolor{ return Objc_getassociatedobject (self, &flashcolorkey);} @end
The above example has several points to note:
1. Key: We notice that the type of key in the function signature is const void *, which means that key is just an address, not the contents of the string, which is also the reason why the Flashcolorkey is not initialized, because we don't care what the specific point is, All we need is the address! If you pass in Setassocaitedobject with Flashcolorkey, the Get method will be nil. The correct one should be the incoming address &flashcolorkey.
2. Policy: The policy here is the same as the retain, assign, and copy in the attribute declaration, no longer repeat
Let's look at another example from Apple Guide:
#import <Foundation/Foundation.h> #import <objc/runtime.h> int main (int argc, const char * argv[]) {@auto Releasepool {/*seciton 0. Key and value*/for associated datastatic char overviewkey;static const char *myownkey = "Videoproperty\0"; static const char Intvaluekey = ' I '; Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", nil]; For the purposes of illustration, use initwithformat:to ensure//We get a deallocatable string NSString *overvi EW = [[NSString alloc] initwithformat:@ "%@", @ "first three numbers"]; NSString *videokeyvalue = @ "This is a video"; NSNumber *intvalue = [[NSNumber alloc]initwithint:5];/*section 1. Associated Data Settings section * /Objc_setassociatedobject (Array, &overviewkey, overview, objc_association _retain); [Overview Release];objc_setassociatedobject (Array, Myownkey, Videokeyvalue, objc_association_retain); objc_se Tassociatedobject (Array, &intvaluekey, Intvalue, Objc_association_retain);/*section 3. Associated Data Query section * /NSString *associatedobject = (NSString *) objc_getassociatedobject (array, &overviewkey); NSLog (@ "Associatedobject:%@", associatedobject); NSString *associatedobject2 = (NSString *) objc_getassociatedobject (array,Myownkey); NSLog (@ "Video Key value is%@", associatedObject2); NSString *ASSOBJECT3 = (NSString *) objc_getassociatedobject (array,&myownkeyif (ASSOBJECT3) {NSLog (@ "will not enter here! ASSOBJECT3 should be nil!");} Else{nslog (@ "OK. Through Myownkey's address is not data!"); NSNumber *asskeyvalue = (NSNumber *) objc_getassociatedobject (array, &intvaluekey); NSLog (@ "Int value is%d", [Asskeyvalue intvalue]);/*section 3. Associated Data Cleansing section * /Objc_setassociatedobject (Array, &overviewkey,Nil,objc_association_assign); Objc_setassociatedobject (array, Myownkey,Nil,objc_association_assign); Objc_setassociatedobject (array, &intvaluekey, nil, objc_association_assign); [Array release]; } return 0;}
How to add properties dynamically in "original" iOS--association (e.g. adding member variables to the category)