(bug更正)利用KVC和associative特性在NSObject中存储键值
KVC
一直没仔细看过KVC的用法,想当然的认为可以在NSObject对象中存入任意键值对,结果使用时碰到问题了。
一个简单的位移动画:
CAKeyframeAnimation *keyPosi=[CAKeyframeAnimation animationWithKeyPath:@"position"];
keyPosi.path=path.CGPath;
keyPosi.delegate=self;
[label.layer addAnimation:keyPosi forKey:@"x"];
我想要在动画结束后把UILabel从屏幕上移除,于是加上动画结束后的回调:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
}
然而看上去从回调方法的参数中似乎无法得到UILabel对象(也许提供的有api可以获得UIlabel,希望各位看官不吝赐教),如果只是为了在这里得到UILabel对象而去设置一个全局变量指针指向它感觉没必要,这时候我想起了KVC,于是在创建动画的时候加上一句:
[keyPosi setValue:label forKey:@"xx"];
这样在动画结束的回调方法中可以通过anim参数获得UILabel对象了:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (flag) {
UIView *vie=[anim valueForKey:@"xx"];
[vie removeFromSuperview];
}
}
效果已经实现,然而KVC是这样使用的吗?再看一个例子:
NSObject *obj = [[NSObject alloc] init];
[obj setValue:@"asd" forKey:@"xx"];
运行报错:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0xf65f090> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key xx.'
原来setValue:forKey、setValue:forKeyPath的key(Path)参数不是随便写的,必须是类中定义的成员变量名(或者实例方法),这篇文章写的很清楚:http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html。话说回来,为啥前面CAKeyframeAnimation使用的setValue:forKey的key可以成功设置?类里肯定没有xx这个成员变量。继续查资料发现有个方法setValue:forUndefinedKey:,
setValue:forUndefinedKey:
Invoked by setValue:forKey: when it finds no property for a given key.
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
Discussion
Subclasses can override this method to handle the request in some other way. The default implementation raises an NSUndefinedKeyException.
当setValue方法的key参数在类中找不到对应成员时,会调用这个方法,重写它可以阻止抛出NSUndefinedKeyException异常。
这样看来,CAKeyFrameAnimation类(或者它的父类)应该是重写了setValue:forUndefinedKey:,然而方法里是怎样处理从而使得valueForKey可以正确取到值呢?
associative
objective-c的扩展机制有两个特性:category和associative。category扩展类别,associative扩展属性。使用associative需要导入<objc/runtime.h>头文件。
利用category扩展NSObject类别,利用associative和setValue:forUndefinedKey:让NSObject能够存入任意键值对。
#import <objc/runtime.h>
@interface NSObject (KVC)
- (id)valueForUndefinedKey:(NSString *)key;
- (void)setValue:(id)value forUndefinedKey:(NSString *)key;
@end @implementation NSObject(KVC)
- (id)valueForUndefinedKey:(NSString *)key{
return objc_getAssociatedObject(self, key);
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if ([value isKindOfClass:[NSString class]]) {
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY_NONATOMIC);
}else{
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }
@end
现在再试下:
NSObject *obj = [[NSObject alloc] init];
[obj setValue:@"asd" forKey:@"xx"];
NSLog(@"%@",[obj valueForKey:@"xx"]);
可以输出了吧。
更正!
对各位说声抱歉,之前犯了一个想当然的错误,setValue:forUndefinedKey:和valueForUndefinedKey:两个方法中的key不能直接传给setAssociatedObject和getAssociatedObject方法,如下所示的做法是错误的:
NSString *a=[NSString stringWithFormat:@"hello"];
NSString *b=[NSString stringWithFormat:@"hello"];
[self setValue:@"cannotfind" forKey:a];
NSString *result=[self valueForKey:b];
NSLog(@"%@",result);
这里result是空,因为a、b是两个不同指针,setAssociatedObject和getAssociatedObject使用了不同的key。正确的做法是定义全局静态变量作为key:
static NSString *kHello=@"hello";
[self setValue:@"canfind" forKey:kHello];
NSString *result=[self valueForKey:kHello];
NSLog(@"%@",result);
或者这样:
static char kHello;
- (id)valueForUndefinedKey:(NSString *)key{
const void *newkey=nil;
if ([key isEqualToString:@"hello"]) {
newkey=&kHello;
}else{
NSAssert(false, @"undefined key");
}
return objc_getAssociatedObject(self, newkey);
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
const void *newkey=nil;
if ([key isEqualToString:@"hello"]) {
newkey=&kHello;
}else{
NSAssert(false, @"undefined key");
}
if ([value isKindOfClass:[NSString class]]) {
objc_setAssociatedObject(self, newkey, value, OBJC_ASSOCIATION_COPY_NONATOMIC);
}else{
objc_setAssociatedObject(self, newkey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
(bug更正)利用KVC和associative特性在NSObject中存储键值的更多相关文章
- 利用js对象的特性,去掉数组中的重复项
- 利用js取到下拉框中选择的值
现在的需求是:下拉框中要是选择加盟商让其继续选择学校,要是选择平台管理员则不需要选择学校.隐藏选择下拉列表. 选择枚举值: /// <summary> /// 平台角色 /// </ ...
- 利用pandas映射替换两个字典中的映射值
在公司处理报表,中英文映射表与数值表替换 import pandas as pd data = { "a":"值一", "b":" ...
- iOS - KVC 键值编码
1.KVC KVC 是 Key-Value Coding 的简写,是键值编码的意思,属于 runtime 方法.Key Value Coding 是 cocoa 的一个标准组成部分,是间接给对象属性设 ...
- [原创]obj-c编程16:键值编码(KVC)
原文链接:obj-c编程16:键值编码(KVC) 我们可以借助obj-c中的键值编码(以后简称KVC,Key-Value Coding)来存取类的属性,通过指定所要访问的属性名字符串标示符,可以使用存 ...
- obj-c编程16:键值编码(KVC)
我们可以借助obj-c中的键值编码(以后简称KVC,Key-Value Coding)来存取类的属性,通过指定所要访问的属性名字符串标示符,可以使用存取器方法来获取或设置类的属性.下面的例子,借助于K ...
- ios中键值编码kvc和键值监听kvo的特性及详解
总结: kvc键值编码 1.就是在oc中可以对属性进行动态读写(以往都是自己赋值属性) 2. 如果方法属性的关键字和需要数据中的关键字相同的话 ...
- 利用atimicInteger cas的特性实现一个锁
利用atimicInteger cas的特性实现一个锁 主要是使用的是 atomicIntegerAPI 的compareAndSet()方法,让线程不在阻塞,获取不到直接失败. 我们先定义一个异常类 ...
- 《芒果TV》UWP版利用Windows10通用平台特性,率先支持Xbox One平台
在Windows开发者中心开放提交Xbox平台应用之后,<芒果TV>UWP版迅速更新v3.1.2版,通过升级兼容目标,利用Windows10通用平台特性,率先覆盖Xbox平台用户. 芒果T ...
随机推荐
- 笔记本PS/2键盘无法使用,试下这个方法
用360清理了一下系统,再开机键盘就不灵了,鼠标却可以用. 打开设备管理器,看到PS/2标准键盘有个黄色的感叹号. 属性显示PS/2 标准键盘 Windows 无法加载这个硬件的设备驱动程序.驱动程序 ...
- 安卓开发中Theme.AppCompat.Light的解决方法
styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错 ...
- 通过源码看android系列之AsyncTask
整天用AsyncTask,但它的内部原理一直没有特意去研究,今天趁着有时间,码下它的原理. 具体用法就不再说明,相信大家已经用得很熟练了,我们今天就从它怎么运行开始说.先新建好我们的AsyncTask ...
- 8086FLAG寄存器
8086中的FLAG寄存器也就是状态标志位寄存器.它用来存储一些指令的计算结果,比如加法减法中的进位:为CPU运行某些命令提供根据,比如DF它决定是往前走指针还是向后走指针:总之状态寄存器存放的被称为 ...
- the Linux Kernel: Traffic Control, Shaping and QoS
−Table of Contents Journey to the Center of the Linux Kernel: Traffic Control, Shaping and QoS 1 Int ...
- java程序员从笨鸟到菜鸟系列
http://blog.csdn.net/csh624366188/article/category/888600
- linux-0.11内核 调试教程+GCC源代码
http://pan.baidu.com/share/home?uk=453348606&view=share#category/type=0 http://blog.csdn.net/old ...
- oracle合并查询
1). Union 该操作符用于取得两个结果集的并集.当使用该操作符时,会自动去掉结果集中重复行. 2).union all 该操作符与union相似,但是它不会取消重复行,而且不会排序. 3). I ...
- CentOS7使用Redis
使用Python操作Redis 安装pip # yum install python-pip 升级pip # pip install --upgrade pip 安装redis-py库 # pip i ...
- mybatis根据property获取column
mybatis根据property获取column mybatis根据类的属性获取xml文件中对应的column mybatis获取xml文件中property对应的column >>&g ...