Today saw the source of Fdtemplatelayoutcell, the category is quite frequent use of related objects, make notes!!! Learning Routines
Main functions:
void Const void *constvoid *void objc_removeassociatedobjects (ID object );
Basic Note: The association object is the nsmultabledictionary of the runtime realm
equivalent to Setvalue:forkey to associate a value object The Objc_associationpolicy property used to read The object is to set the value of the property within the object, i.e. Assgin, (retain,nonatomic) ... function to remove an associated object, or use the Objc_setassociatedobject function to set the associated object specified by key to nil.
Related parameters
Key: To guarantee global uniqueness, key is associated with an object that is a one by one correspondence relationship. Must be globally unique. @selector(methodName) is usually used as key. Value: The object to be associated with. Policy: Associate policies. There are five kinds of correlation strategies. Objc_association_assign is equivalent to @property (ASSIGN). Objc_association_retain_nonatomic is equivalent to @property (strong, nonatomic). Objc_association_copy_nonatomic is equivalent to @property (COPY, nonatomic). Objc_association_retain is equivalent to @property (strong,atomic). Objc_association_copy is equivalent to @property (copy, Atomic).
Usage scenarios: Associative objects are equivalent to instance variables, and in categories (also called categories), you cannot create instance variables, and the associated objects can solve this problem. (corresponding attributes, with corresponding attributes of the runtime solution, this article is to teach related object learning)
Basic use: Create UIButton categories for example
////uibutton+block.h//Sectiondemo////Created by HF on 2017/7/13.//Copyright 2017 Hf-liqun. All rights reserved.//#Import<UIKit/UIKit.h>typedefvoid(^Btnblock) (id sender);InterfaceUIButton (Block)- (void) Handelwithblock: (btnblock) block; @end////UIBUTTON+BLOCK.M//Sectiondemo////Created by HF on 2017/7/13.//Copyright 2017 Hf-liqun. All rights reserved.//#Import"Uibutton+block.h"#Import<objc/runtime.h>@implementation UIButton (Block)- (void) Handelwithblock: (btnblock) block {if(block) { objc_setassociatedobject(self,@selector(btnaction:), block, Objc_association_reta In_nonatomic); } [Self addtarget:self action:@selector(btnaction:) forcontrolevents:uicontroleventtouchupinside];}- (void) Btnaction: (ID) sender{Btnblock block= objc_getassociatedobject(self,@selector(btnaction:)); if(block) {block (sender); }} @end
Notice that the bright pink portion of the marker is the second parameter of the const void *key type, which usually takes a static variable as a keyword that you can create yourself or use "@selector(btnaction:) ".
Set the static variable to do the keyword yourself:
In Fdtemplatelayoutcell the second parameter is further optimized, and _cmd is replaced by the &Btnkey or @selector(methodName).
Because:
_cmd represents the selectorof the current method in the Objective-c method, as with self represents an object instance of the current method invocation.
It is emphasized that currently, the scope of _cmd is only in the current method, pointing directly to the current method name @selector. For example:
The advantage of this approach is that the method is relatively simple and has a good readability. Not easy to write wrong key.
Use of _cmd in reference Fdtemplatelayoutcell
At this point, there are three ways to set up key keys for the associated object:
(1) Static variables &Btnkey
(2) @selector (MethodName)
(3) _cmd
Reference
(1) http://blog.csdn.net/luoyeffcs/article/details/27967193
(2) http://blog.jobbole.com/79570/
(3) http://www.jianshu.com/p/46cd19bc4b6b
(4) http://www.jianshu.com/p/fdb1bc445266
Learning about IOS Objc_setassociatedobject associated objects