runtime关联属性

关联API介绍


咱们先看看关联API,只有这三个API,使用也是很是简单的: 安全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
/**
* Sets an associated value for a given object using a given key and association policy.
*
* @param object The source object for the association.
* @param key The key for the association.
* @param value The value to associate with the key key for object. Pass nil to clear an existing association.
* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*
* @see objc_setAssociatedObject
* @see objc_removeAssociatedObjects
*/
void objc_setAssociatedObject ( id object , const void *key , id value , objc_AssociationPolicy policy )
 
/**
* Returns the value associated with a given object for a given key.
*
* @param object The source object for the association.
* @param key The key for the association.
*
* @return The value associated with the key \e key for \e object.
*
* @see objc_setAssociatedObject
*/
id objc_getAssociatedObject ( id object , const void *key )
 
/**
* Removes all associations for a given object.
*
* @param object An object that maintains associated objects.
*
* @note The main purpose of this function is to make it easy to return an object
*  to a "pristine state”. You should not use this function for general removal of
*  associations from objects, since it also removes associations that other clients
*  may have added to the object. Typically you should use \c objc_setAssociatedObject
*  with a nil value to clear an association.
*
* @see objc_setAssociatedObject
* @see objc_getAssociatedObject
*/
void objc_removeAssociatedObjects ( id object )
 

实际上,咱们几乎不会使用到objc_removeAssociatedObjects函数,这个函数的功能是移除指定的对象上全部的关联。 ide

设置关联值

对于设置关联,咱们须要使用下面的API关联起来: 函数

1
2
3
 
void objc_setAssociatedObject ( id object , const void *key , id value , objc_AssociationPolicy policy )
 

参数说明: ui

  • object:与谁关联,一般是传self
  • key:惟一键,在获取值时经过该键获取,一般是使用static const void *来声明
  • value:关联所设置的值
  • policy:内存管理策略,好比使用copy

获取关联值

若是咱们要获取所关联的值,须要经过key来获取,调用以下函数: this

1
2
3
 
id objc_getAssociatedObject ( id object , const void *key )
 

参数说明: atom

  • object:与谁关联,一般是传self,在设置关联时所指定的与哪一个对象关联的那个对象
  • key:惟一键,在设置关联时所指定的键

关联策略

咱们先看看设置关联时所指定的policy,它是一个枚举类型,看官方说明: spa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
/**
* Policies related to associative references.
* These are options to objc_setAssociatedObject()
*/
typedef OBJC_ENUM ( uintptr_t , objc_AssociationPolicy ) {
     OBJC_ASSOCIATION_ASSIGN = 0 ,            /**< Specifies a weak reference to the associated object. */
     OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1 , /**< Specifies a strong reference to the associated object.
                                            *   The association is not made atomically. */
     OBJC_ASSOCIATION_COPY_NONATOMIC = 3 ,    /**< Specifies that the associated object is copied.
                                            *   The association is not made atomically. */
     OBJC_ASSOCIATION_RETAIN = 01401 ,        /**< Specifies a strong reference to the associated object.
                                            *   The association is made atomically. */
     OBJC_ASSOCIATION_COPY = 01403            /**< Specifies that the associated object is copied.
                                            *   The association is made atomically. */
} ;
 

咱们说明一下各个值的做用: .net

  • OBJCASSOCIATIONASSIGN:表示弱引用关联,一般是基本数据类型,如int、float
  • OBJCASSOCIATIONRETAIN_NONATOMIC:表示强(strong)引用关联对象
  • OBJCASSOCIATIONCOPY_NONATOMIC:表示关联对象copy
  • OBJCASSOCIATIONRETAIN:表示强(strong)引用关联对象,但不是线程安全的
  • OBJCASSOCIATIONCOPY:表示关联对象copy,但不是线程安全的

扩展属性


咱们来写一个例子,扩展UIControl添加Block版本的TouchUpInside事件。 线程

扩展头文件声明: code

1
2
3
4
5
6
7
8
9
10
11
 
#import <UIKit/UIKit.h>
 
typedef void ( ^ HYBTouchUpBlock ) ( id sender ) ;
 
@interface UIControl ( HYBBlock )
 
@property ( nonatomic , copy ) HYBTouchUpBlock hyb_touchUpBlock ;
 
@end
 

扩展实现文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
#import "UIControl+HYBBlock.h"
#import <objc/runtime.h>
 
static const void *sHYBUIControlTouchUpEventBlockKey = "sHYBUIControlTouchUpEventBlockKey" ;
 
@implementation UIControl ( HYBBlock )
 
- ( void ) setHyb_touchUpBlock : ( HYBTouchUpBlock ) hyb _ touchUpBlock {
   objc_setAssociatedObject ( self ,
                           sHYBUIControlTouchUpEventBlockKey ,
                           hyb_touchUpBlock ,
                           OBJC_ASSOCIATION_COPY ) ;
  
   [ self removeTarget :self
              action : @selector ( hybOnTouchUp : )
    forControlEvents :UIControlEventTouchUpInside ] ;
  
   if ( hyb_touchUpBlock ) {
     [ self addTarget :self
             action : @selector ( hybOnTouchUp : )
   forControlEvents :UIControlEventTouchUpInside ] ;
   }
}
 
- ( HYBTouchUpBlock ) hyb _ touchUpBlock {
   return objc_getAssociatedObject ( self , sHYBUIControlTouchUpEventBlockKey ) ;
}
 
- ( void ) hybOnTouchUp : ( UIButton * ) sender {
   HYBTouchUpBlock touchUp = self . hyb_touchUpBlock ;
  
   if ( touchUp ) {
     touchUp ( sender ) ;
   }
}
 
@end
 

使用起来很简单吧!!!


用法,在使用到的类进行 引用category;

   

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [backBtn setHyb_touchUpBlock:^(id btn){

        NSLog(@"backk");

    }];
就这样愉快的使用啦
相关文章
相关标签/搜索