[Objective-c] Association (objc_setassociatedobject, Objc_getassociatedobject, objc_removeassociatedobjects)

Source: Internet
Author: User

Association Association refers to associating two objects together so that one of them is part of another object.
The affinity attribute is only available on Mac OS X V10.6 and later versions. In addition to the class's definition for adding additional storage space to the class, we can add storage space to its 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.
Listing 7-1 associating a string to an array [CPP]View Plaincopy
  1. static char Overviewkey;
  2. Nsarray * Array =[[nsarray alloc] initwidthobjects:@"One", @"one", @"three", nil];
  3. For demonstration purposes, use Initwithformat: To ensure that the string can be destroyed
  4. NSString * overview = [[NSString alloc] initwithformat:@"@" @ "Firstthree 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 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 when getting the associated object 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:
[CPP]View Plaincopy
    1. NSString * Associatedobject = (NSString *) objc_getassociatedobject (array, &oveviewkey);
Disconnecting the association disconnects is by using the Objc_setassociatedobject function, passing 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:
[CPP]View Plaincopy
    1. 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 below combines the preceding code.
[CPP]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. #import <objc/runtime.h>
  3. int main (int argc, const char* argv[])
  4. {
  5. NSAutoreleasePool * Pool = [[NSAutoreleasePool] alloc init];
  6. static char overviewkey;
  7. Nsarray *array =[[nsarray alloc] initwidthobjects:@"One", @"one", @"three", nil];
  8. //For demonstration purposes, use Initwithformat here: to ensure that the string can be destroyed
  9. NSString * overview = [[NSString alloc] initwithformat:@"@" @ "Firstthree 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. }
 

[Objective-c] Association (objc_setassociatedobject, Objc_getassociatedobject, objc_removeassociatedobjects)

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.