IOS Association object


1. Category and extension

1.1 category: category

  • Specifically used to add new methods to classes;
  • You cannot add a member attribute to a class, add a member variable, or get it,be careful:You can use runtime to add attributes to a classification
  • Used in classification@propertyDefining variables only generates variablessetter,getterCannot generate method implementations and underlined member variables.

1.2 extension: class extension

  • It can be said to be a special classification or anonymous classification;
  • You can add a member attribute to a class, but it is a private variable;
  • You can add methods to classes, which are also private methods.

2. Associated object

Member properties added in the classification are not generatedsetter,getterSo we can useruntimeAssociate objects to add properties to members.

@interface LGPerson (LG)

@property (nonatomic, copy) NSString *cate_name;
@property (nonatomic, assign) int cate_age;

@implementation LGPerson (LG)

- (void)setCate_name:(NSString *)cate_name{
     1: Object
     2: Identifier
     3: value
     4: Tactics
    objc_setAssociatedObject(self, "cate_name", cate_name, OBJC_ASSOCIATION_COPY_NONATOMIC);

- (NSString *)cate_name{
    return  objc_getAssociatedObject(self, "cate_name");

2.1 associated object setting process

Enter the source code to viewobjc_setAssociatedObjectMethod implementation will eventually come_object_set_associative_reference(id object, const void *key, id value, uintptr_t policy)Within the method:

IOS Association object

Set value process 01 png

According to the source code, the important code is169~176that ‘s ok.

  • Line 169:
    takeobjectPackage intodisguised
  • Line 171:
    takepolicy, valuePackage intoassociation;
  • Line 174:
    IOS Association object


    If you are currently usingretain/copyModify to process value. If not, it will not be processed. In short, it is to judge the value.

  • Line 176 (open):

    IOS Association object

    Setting process 02 png

    IOS Association object

    Manager initialization png

First, when associating objectsAssociationsManagerObject, and thenmanagerWill be of great significance to our entire projectAssociationsHashMapGet. Because of thisAssociationsHashMapIt is used by us in the whole project. We will continue to add, delete, modify and check in the project, so we are doing itAssociationsManagerLocking is required during initialization.

IOS Association object


Then we passedtry_emplaceFrom summary tableassociationsTake the data, intry_emplaceIn, we define abucketAnd then callLookupBucketForTo pass it onkeymatchingbucket

IOS Association object


Then you get a result:

IOS Association object


Print it out for true, then call it.setHasAssociatedObjectsMethod, byisaThe association to the current object is set totrue, this time you will findhas_assocAndnonpointerisaThe properties are as like as two peas.

IOS Association object


Then we passedrefs_resultcalltry_emplaceContinue to passkeymatchingbucket, because the first time you insert an emptybucket, so called againinsertMethod insertionObjectAssociationMap,ObjectAssociationMapContainsvalueandpolicyIn this way, you can find it next time and returntrueYes.

IOS Association object


If it isfalse, that is!result.secondThe case is to callswapFunction to move.

IOS Association object

Value is null png

WhenvalueIf it is empty, the current associated object will be cleared.

2.1.1 summary

  • Set value process
  1. Create aAssociationsManagerManagement class, throughget()To obtain a unique global static hash map;
  2. Judge whether the inserted association value exists. If it exists, go down and clean up the iterator without saving the value;
  3. Create an emptyObjecAssociationMapGet the key value pair of the query;
    4. If the key is not found, insert an empty keybucketGo in and return;
    5. The marked object has an associated object;
    6. Use the current modification strategy and value to form aObjecAssociationReplace the original emptybucket;
    7. Mark itObjecAssociationMapThe first time is false.

2.2 value taking process

IOS Association object

Value png

By callingobjc_getAssociatedObjectAfter taking the value, you will come to_object_get_associative_referenceWithin the method.
technological process:

  1. Create aAssociationsManagerManagement class, throughget()To obtain a unique global static hash map;
  2. according toDisguiserPtrfindObjecAssociationHashMapMediumiteratorIterative query;
  3. If the iterative query is not the last acquisition: object association map (there are policies and values);
  4. Find the iterative query of objectassociationmap and obtain a value modified by attribute modifier;
    5. Return_ value。

2.3 structure diagram of associated objects

IOS Association object

Associated object structure diagram png

It can be seen from the figure that the associated object is actually a two-layer hash table, which is processed in two layers during access, similar to a two-dimensional array.

Recommended Today

Tutorial on sending e-mail using net:: SMTP class in Ruby

Simple Mail Transfer Protocol(SMTP)SendE-mailAnd routing protocol processing between e-mail servers. RubyIt provides the connection of simple mail transfer protocol (SMTP) client of net:: SMTP class, and provides two new methods: new and start New takes two parameters: Server name defaults to localhost Port number defaults to 25 The start method takes these parameters: Server – […]