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

Source: Internet
Author: User

category and associative as the extension mechanism of the objective-c two features, category is the type, you can extend the method through it, associative, you can extend the property through it, in iOS development, Perhaps the category is more common, relative associative, use less, to use it must be used <objc/runtime.h> header file, then you can freely use Objc_getassociatedobject and OBJC _setassociatedobject, let's look at these two methods:

1. Create an association: parameter settings ( source object, keyword, associated object, and an associated policy)

2. Get the associated object: parameter settings ( source object, keyword)

In addition, there is another way: You can 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".

Note: Disconnecting can use Objc_setassociatedobject ( source object, keyword, associated object, and an associated policy), and the associated object will pass in nil;

  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.

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 classWith 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 associationCreate 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. Staticchar, @, @, nil];
  2. For demonstration purposes, use Initwithformat: To ensure that the string can be destroyed,@//(1)  overview is still available //(2) overview  not available
    at (1), the string overview is still available, This is because the Objc_association_retain policy indicates that the array is to hold 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.
/span> Get 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:
[CPP]View Plaincopy
    1. NSString * Associatedobject = (NSString *) objc_getassociatedobject (array, &oveviewkey);
Disconnect AssociationDisconnecting 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:
[CPP]View Plaincopy
    1. Objc_setassociatedobject (array, &overviewkey, nil, objc_association_assign);
    in which the associated object is nil, the association policy does not matter at this point.
    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".
/span> A complete instance of the program The following program synthesizes the preceding code.
[CPP]View Plaincopy
  1. #import <Foundation/Foundation.h>#import <objc/runtime.h>IntIntConstCharchar, @, @
  2. &NBSP;&NBSP;
  3. ,@, associatedobject);   
  4. Return}

[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.