From: http://blog.csdn.net/onlyou930/article/details/9299169classification: Objective-C2013-07-11 11: 54 3420 people read comments (0) collection reports Association is to associate two objects, so that one of the objects as part of another object.
The Association feature is available only in Mac OS X v10.6 and later versions. In addition to the class definition, we can add additional storage space for the class to use association. Instead of modifying the class definition, we can increase the storage space for its objects. This is very useful when we cannot access the class source code or when considering binary compatibility.
Association is based on keywords. Therefore, we can add any number of associations to any object, and each object can use different keywords. Association ensures that the associated object is available throughout the lifecycle of the associated object (in the garbage collection environment, the resource cannot be recycled ).
To create an association, use the objective-C Runtime function objc_setassociatedobject to associate an object with another object. This function requires four parameters: source object, keyword, associated object, and an association policy. Of course, the keywords and associated policies here need to be further discussed.
■ The keyword is a void pointer. Each associated keyword must be unique. Static variables are usually used as keywords.
■ The association policy indicates whether the related objects are associated by assigning values, retaining references, or replicating objects. In addition, the Association is atomic or non-atomic. The association policy here is similar to the attribute declaration. This association policy is represented by a predefined constant.
The following code associates a string with an array.
List 7-1 associates a string with an Array
[CPP]View plaincopy
- Static char overviewkey;
- Nsarray * array = [[nsarray alloc] initwidthobjects: @ "one", @ "two", @ "three", nil];
- // For demonstration purpose, initwithformat: is used to ensure that the string can be destroyed.
- Nsstring * Overview = [[nsstring alloc] initwithformat: @ "@", @ "first three numbers"];
- Objc_setassociatedobject (array, & overviewkey, Overview, objc_association_retain );
- [Overview release];
- // (1) overview is still available
- [Array release];
- // (2) Overview unavailable
At (1), the string overview is still available because the objc_association_retain policy specifies that the array must retain the relevant objects. When the array is destroyed, the overview in (2) will be released and destroyed. If you want to use overview at this time, for example, to output the overview value through log, a runtime exception will occur.
Use the objective-C function objc_getassociatedobject to obtain the associated object. Next, we will use the code 7-1 in the above list to obtain the strings associated with the array:
[CPP]View plaincopy
- Nsstring * associatedobject = (nsstring *) objc_getassociatedobject (array, & oveviewkey );
To disconnect an association, use the objc_setassociatedobject function and pass in the nil value.
Next, we can use the following code to disconnect the association between the overview string and arry in the program list 7-1:
[CPP]View plaincopy
- Objc_setassociatedobject (array, & overviewkey, nil, objc_association_assign );
The associated object is nil, and the associated policy is irrelevant.
You can use the objc_removeassociatedobjects function to disconnect all associations. Generally, this function is not recommended because it disconnects all associations. This function is used only when the object needs to be restored to the original state.
The program under a complete instance program integrates the previous code.
[CPP]View plaincopy
- # Import <Foundation/Foundation. h>
- # Import <objc/runtime. h>
- Int main (INT argc, const char * argv [])
- {
- NSAID utoreleasepool * Pool = [[NSAID utoreleasepool] alloc init];
- Static char overviewkey;
- Nsarray * array = [[nsarray alloc] initwidthobjects: @ "one", @ "two", @ "three", nil];
- // For demonstration purpose, initwithformat: is used to ensure that the string can be destroyed.
- Nsstring * Overview = [[nsstring alloc] initwithformat: @ "@", @ "first three numbers"];
- Objc_setassociatedobject (array, & overviewkey, Overview, objc_association_retain );
- [Overview release];
- Nsstring * associatedobject = (nsstring *) objc_getassociatedobject (arrray, & overviewkey );
- Nslog (@ "associatedobject: % @", associatedobject );
- Objc_setassociatedobject (array, & overviewkey, nil, objc_association_assign );
- [Array release];
- [Pool drain];
- Return 0;
- }