Association
Association refers to associating two objects together so that one of the objects is part of another object.
The affinity attribute is only available on Mac OS X V10.6 and later versions.
Add extra storage space for a class beyond the definition of the class
With correlation, we can add storage space to objects without modifying the definition of the class. This is useful when we are unable to access the source code of the class or when considering binary compatibility.
Associations are based on keywords, so we can add any number of associations to any object, each with a different keyword. An association is a guarantee that the associated object is available throughout the life cycle of the associated object (and does not cause the resource to be non-recyclable in a garbage-collected environment).
Create an association
Create an association run-time function to use to OBJECTIVE-C: Objc_setassociatedobject to associate an object with another object. The function requires four parameters: the source object, the keyword, the associated object, and an associated policy. Of course, the keywords and associated policies here are needed for further discussion.
The keyword is a type of void pointer. Each associated keyword must be unique. Static variables are usually used as keywords.
The association policy indicates whether the related object is associated by assignment, preserving the reference or copying it, and whether the association is atomic or non-atomic. The correlation policy here is similar to the Declaration property. This association strategy is represented by the use of a pre-defined represented.
The following code shows how to associate a string to an array.
static char overviewKey;
NSArray * array = [[NSArray alloc] initWidthObjects: @ "One", @ "Two", @ "Three", nil];
// For demonstration purposes, use initWithFormat: 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 is not available
at (1), the string overview is still available because the Objc_association_retain policy indicates that the array retains the associated object. When an array of arrays is destroyed, the overview is released at (2) and is destroyed. If you want to use overview at this point, for example, if you want to output the value of overview through log, a run-time exception occurs.
Gets the associated object
Gets the associated object when using the Objective-c function Objc_getassociatedobject. Next to the code in listing 7-1 above, we can use the following code to get the string associated with the array:
NSString * Associatedobject = (NSString *) objc_getassociatedobject (array, &oveviewkey);
Disconnect Association
Disconnecting is using the Objc_setassociatedobject function to pass in the nil value.
Next to the program in Listing 7-1, we can use the following code to break the association between the string overview and Arry:
Objc_setassociatedobject (array, &overviewkey, nil, objc_association_assign);
Where the associated object is nil, then the association policy does not matter.
Use the function objc_removeassociatedobjects to disconnect all associations. It is generally not recommended to use this function because he disconnects all of the associations. This function is only used when the object needs to be restored to the "original state".
A complete instance of the program
The following program synthesizes the preceding code.
#import <Foundation / Foundation.h>
#import <objc / runtime.h>
int main (int argc, const char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool] alloc init];
static char overviewKey;
NSArray * array = [[NSArray alloc] initWidthObjects: @ "One", @ "Two", @ "Three", nil];
// For demonstration purposes, use initWithFormat: 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;
}
[Objective-c] Association (objc_setassociatedobject, Objc_getassociatedobject, objc_removeassociatedobjects)