Sometimes we need to store some extra information in an existing class, usually by inheriting a subclass and then defining the newly added attributes, but if we add a class to each of the required classes it's too cumbersome, OBJC provides an attribute of the associated property that can be associated with an object (done). NET friends must be familiar with the attached property, OBJC the associated property with. NET attached properties are somewhat similar)
Using the associated property is simple, here are a few main functions
// Set association properties Objc_setassociatedobject (id object , Span style= "color: #0000ff;" >const void *key, id // Get the associated property id result = Objc_getassociatedobject (id object , const void *key) // Remove all attached properties objc_removeassociatedobjects (id object )
When we declare attributes, we declare: copy,retain,assign memory management mode, when setting the associated properties also, that is, objc_setassociatedobject
| Association type |
Equivalent @property property |
|
Objc_association_assign |
Assign |
|
Objc_association_retain_nonatomic |
nonatomic , retain |
|
Objc_association_copy_nonatomic |
nonatomic, copy |
|
Objc_association_retain |
Retain |
|
Objc_association_copy |
Copy |
The associated property is appended to the nsdictionary, which is stored by key-value pairs, but one thing that is different from nsdictionary is that the key (key) used to set the associated object is an opaque pointer (opaque pointer). Nsdictionary determines whether the IsEqual method of two keys is equal, and the associated object only determines whether the address of the two key is equal, and does not compare the object's specific value
Look at the following example
NSString *key1 =@"AAA"; NSString*key2 = [NSString stringWithFormat:@"AAA"]; NSString*key3 =@"BBB"; NSObject*obj =[[NSObject alloc] init]; Objc_setassociatedobject (obj, Key1. Utf8string,@"Aaa1", objc_association_retain_nonatomic); Objc_setassociatedobject (obj, Key2. Utf8string,@"Aaa2", objc_association_retain_nonatomic); Objc_setassociatedobject (obj, Key3. Utf8string,@"BBB", objc_association_retain_nonatomic); IDValue1 =objc_getassociatedobject (obj, Key1. utf8string); IDvalue2 =objc_getassociatedobject (obj, Key2. utf8string); IDValue3 =objc_getassociatedobject (obj, Key3. utf8string); NSLog (@"value1 =%@", value1); NSLog (@"value2 =%@", value2); NSLog (@"value3 =%@", value3);
Output
Here the Key1 and Key2 are @ "AAA", but the value1 and value2 are not the same, because Key1 and Key2 address is not the same, in the match when the associated object as a different key, So we usually use a static global variable as a key when defining the key of the associated object, so that the address will be consistent
Static Char*key ="AAA"; NSObject*obj =[[NSObject alloc] init]; Objc_setassociatedobject (obj, key,@"Aaa1", objc_association_retain_nonatomic); Objc_setassociatedobject (obj, key,@"Aaa2", objc_association_retain_nonatomic); IDValue1 =objc_getassociatedobject (obj, key); //overwrite the original value IDvalue2 =objc_getassociatedobject (obj, key); NSLog (@"value1 =%@", value1); NSLog (@"value2 =%@", value2);
"IOS" Association properties Access data