[Objective-C] Association (objc_setassociatedobject, objc_getassociatedobject, objc_removeassociatedobjects) (convert)

Source: Internet
Author: User
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
  1. Static char overviewkey;
  2. Nsarray * array = [[nsarray alloc] initwidthobjects: @ "one", @ "two", @ "three", nil];
  3. // For demonstration purpose, initwithformat: is used to ensure that the string can be destroyed.
  4. Nsstring * Overview = [[nsstring alloc] initwithformat: @ "@", @ "first three numbers"];
  5. Objc_setassociatedobject (array, & overviewkey, Overview, objc_association_retain );
  6. [Overview release];
  7. // (1) overview is still available
  8. [Array release];
  9. // (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
  1. 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
  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.
The program under a complete instance program integrates the previous code.
[CPP]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2. # Import <objc/runtime. h>
  3. Int main (INT argc, const char * argv [])
  4. {
  5. NSAID utoreleasepool * Pool = [[NSAID utoreleasepool] alloc init];
  6. Static char overviewkey;
  7. Nsarray * array = [[nsarray alloc] initwidthobjects: @ "one", @ "two", @ "three", nil];
  8. // For demonstration purpose, initwithformat: is used to ensure that the string can be destroyed.
  9. Nsstring * Overview = [[nsstring alloc] initwithformat: @ "@", @ "first three numbers"];
  10. Objc_setassociatedobject (array, & overviewkey, Overview, objc_association_retain );
  11. [Overview release];
  12. Nsstring * associatedobject = (nsstring *) objc_getassociatedobject (arrray, & overviewkey );
  13. Nslog (@ "associatedobject: % @", associatedobject );
  14. Objc_setassociatedobject (array, & overviewkey, nil, objc_association_assign );
  15. [Array release];
  16. [Pool drain];
  17. Return 0;
  18. }
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.