Objective-c Associated Objects First knowledge

Source: Internet
Author: User

Associated Objects mainly has the following three usage scenarios:

1. Add a private variable for the existing class to help implement the details

2. Add the public attribute to the existing class;

3. Create an associated observer for KVO.

There are 3 association functions:

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);Used to add an associated object to an object, passing in nil to remove an existing association object id objc_getAssociatedObject(id object, const void *key);Used to get the associated object void objc_removeAssociatedObjects(id object);All associated objects that are used to remove an object Note:The objc_removeassociatedobjects function is generally not available because this function removes all associated objects from an object and restores the object to the "original" state. This is likely to remove the associated objects that others have added, which is not what we want. So the general practice is to remove an existing association object by passing in nil to the Objc_setassociatedobject functionthe declaration of the key value:* declare static char Kassociatedobjectkey; use &kassociatedobjectkey* with selector, use the name of the getter method as the key value

Objc_association_assign = 0,//weak reference associated object

Objc_association_retain_nonatomic = 1,//strong reference to associated object, and non-atomic operation

Objc_association_copy_nonatomic = 3,//Copy associated object, and non-atomic operation

Objc_association_retain = 01401,//strong reference to associated object, and atomic operation

Objc_association_copy = 01403//Copy associated object, and atomic operation

 

Objective-c Associated Objects First knowledge

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.