[Objective-C] Association (objc_setAssociatedObject, objc_getAssociatedObject, objc_removeAssociatedObjects), associatedobjects

Source: Internet
Author: User

[Objective-C] Association (objc_setAssociatedObject, objc_getAssociatedObject, objc_removeAssociatedObjects), associatedobjects
Association

Association means to associate two objects so that one object is part of another object.
The Association feature is available only in Mac OS X V10.6 and later versions.

Add additional storage space for the class beyond the class definition

With association, we can increase the storage space for its objects without modifying the class definition. 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 ).

Create Association

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.

Static char overviewKey; NSArray * array = [[NSArray alloc] initWidthObjects: @ "One", @ "Two", @ "Three", nil]; // for demonstration purposes, here 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 is 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.

Obtain the associated object

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:

NSString * associatedObject = (NSString *)objc_getAssociatedObject(array, &oveviewKey);  

  

Disconnect

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:

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.

A complete instance Program

The following program integrates the previous code.

# Import <Foundation/Foundation. h> # import <objc/runtime. h> int main (int argc, const char * argv []) {ngutoreleasepool * pool = [[ngutoreleasepool] alloc init]; static char overviewKey; NSArray * array = [[NSArray alloc] initWidthObjects: @ "One", @ "Two", @ "Three", nil]; // initWithFormat is used for demonstration purposes: NSString * overview = [[NSString alloc] initWithFormat: @ "@", @ "First three numbers"]; objc_setAssociatedObject (array, & overviewKey, overview, OBJC_ASSOCIATION_RETAIN); [overview release]; NSString * associatedObject = (NSString *) values (arrray, & overviewKey); NSLog (@ "associatedObject: % @", associatedObject ); objc_setAssociatedObject (array, & overviewKey, nil, OBJC_ASSOCIATION_ASSIGN); [array release]; [pool drain]; return 0 ;}

  

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.