runtime关联属性示例
前言
在开发中经常需要给已有的类添加方法和属性,但是Objective-C是不允许给已有类通过分类添加属性的,因为类分类是不会自动生成成员变量的。但是,我们可以通过运行时机制就可以做到了。
本篇文章适合新手阅读,手把手教你如何在项目中使用关联属性!
API介绍
我们先看看Runtime提供的关联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函数,这个函数的功能是移除指定的对象上所有的关联。既然我们要添加关联属性,几乎不会存在需要手动取消关联的场合。
设置关联值(Setter)
对于设置关联,我们需要使用下面的API关联起来:
|
1
2
3
|
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
|
参数说明:
object:与谁关联,通常是传selfkey:唯一键,在获取值时通过该键获取,通常是使用static const void *来声明value:关联所设置的值policy:内存管理策略,比如使用copy
获取关联值(Getter)
如果我们要获取所关联的值,需要通过key来获取,调用如下函数:
|
1
2
3
|
id objc_getAssociatedObject(id object, const void *key)
|
参数说明:
object:与谁关联,通常是传self,在设置关联时所指定的与哪个对象关联的那个对象key:唯一键,在设置关联时所指定的键
关联策略
我们先看看设置关联时所指定的policy,它是一个枚举类型,看官方说明:
|
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. */
};
|
我们说明一下各个值的作用:
- OBJC_ASSOCIATION_ASSIGN:表示弱引用关联,通常是基本数据类型,如
int、float,非线程安全 - OBJC_ASSOCIATION_RETAIN_NONATOMIC:表示强(strong)引用关联对象,非线程安全
- OBJC_ASSOCIATION_COPY_NONATOMIC:表示关联对象copy,非线程安全
- OBJC_ASSOCIATION_RETAIN:表示强(strong)引用关联对象,是线程安全的
- OBJC_ASSOCIATION_COPY:表示关联对象copy,是线程安全的
扩展属性
我们来写一个例子,扩展UIControl添加Block版本的TouchUpInside事件。
扩展头文件声明:
|
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
|
runtime关联属性示例的更多相关文章
- EF自动生成的(T4模板) 关联属性元数据修改
为了实现 T4模板关联属性 不要序列化的问题 就是要在具体的 关联属性上面添加一个元数据 这里利用以前的 Newtonsoft.Json 这个框架为例 效果应该为 就是要在关联属性上面添加元数据 [ ...
- CSS3 变形、过渡、动画、关联属性浅析
一.变形 transform:可以对元素对象进行旋转rotate.缩放scale.移动translate.倾斜skew.矩阵变形matrix.示例: transform: rotate(90deg) ...
- Runtime - 关联对象使用方法及注意点
大家都知道在分类里,可以间接的添加属性,运用runtime关联对象. 如下图,只是声明了btnClickedCount的set, get方法而已 并没有生成_btnClickedCount 成员变量, ...
- iOS之Category关联属性
Objective-C /** 原文件 */ // Person.h #import <Foundation/Foundation.h> @interface Person : NSObj ...
- 【iOS】关联属性存取数据
有时候我们需要在现有的类存放一些额外的信息,通常的做法是继承一个子类,然后定义新增加的属性,然而如果我们为每个需要的类都添加一个类显得太麻烦了,objc提供了一个关联属性的特性,可以给一个对象关联一个 ...
- WPF/Silverlight深度解决方案:(一)解锁被Storyboard束缚的关联属性
原文 WPF/Silverlight深度解决方案:(一)解锁被Storyboard束缚的关联属性 如果您在使用WPF/Silverlight进行相关动画开发中使用了Storyboard,并对关联属性进 ...
- grid 布局 属性示例
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- iOS-button利用block封装按钮事件【runtime 关联】
用block封装最常用的就是网络请求的回调,其实也可以结合category封装button的按钮事件,同时利用runtime的对象关联: UIButton+wkjButton.h 文件 #import ...
- Hibernate映射一对多双向关联关系及部门关联属性
一对多双向关联关系:(Dept/Emp的案例) 既可以根据在查找部门时根据部门去找该部门下的所有员工,又能在检索员工时获取某个员工所属的部门. 步骤如下: 1.构建实体类(部门实体类加set员工集合) ...
随机推荐
- Understanding continuations
原文地址http://fsharpforfunandprofit.com/posts/computation-expressions-continuations/ 上一篇中我们看到复杂代码是如何通过使 ...
- 支付顺序-->微信支付到公司账户-->待出票
支付顺序-->微信支付到公司账户-->待出票-->查询所有待出票订单 -->遍历提交订单-->火车票接口放回订单id-->存入order订单表中 -->读取订 ...
- Django中的Form表单
Django中已经定义好了form类,可以很容易的使用Django生成一个表单. 一.利用Django生成一个表单: 1.在应用下创建一个forms文件,用于存放form表单.然后在forms中实例华 ...
- uhttpd配置文件分析
文件位于 /etc/config/uhttpd. root@hbg:/etc/config# cat uhttpd config uhttpd 'main' list listen_ht ...
- openwrt增加串口登录需要密码
https://wiki.openwrt.org/doc/howto/serial.console.password Openwrt 串口默认是没有密码的.Openwrt启动后,一个默认的密码将被启用 ...
- 一步一步学EF系列 【7、结合IOC ,Repository,UnitOfWork来完成框架的搭建】
前言 距离上一篇已经有段时间了,最近这段时间赶上新项目开发,一直没有时间来写.之前的几篇文章,主要把EF的基础都讲了一遍,这批文章就来个实战篇. 个人在学习过程中参考博客: Entity Framew ...
- UNIX基础--用户和基本账户管理
账户类型 系统账户 系统账户运行服务. 系统用户是那些要使用诸如DNS. 邮件, web等服务的用户. 使用帐户的原因就是安全: 如果所有的用户都由超级用户来运行, 那它们就可以不受约束地做任何事情. ...
- automaticallyAdjustsScrollViewInsets (iOS)
[摘要:@当我们正在一个UIViewController中同时建立2个tableView的时间,若是把它们的frame中的Y坐标设置为一样,您大概会发明它们的地位并出有到达您念要的效果.比方第一tab ...
- HttpServletRequest对象(一)
一:HttpServletRequest介绍: 代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中, 二:Request常用的方法 1):获得客户端信 ...
- sftp配置多用户权限
sftp配置多用户权限 工作需要,用户上传文件到目录下,用ftp不太安全,选择sftp.让用户在自己的home目录下活动,不能ssh到机器进行操作. 下面开始干活. 查看ssh版本 ssh - ...