objectforkey 是NSDictionary的方法,valueforkey 是KVC的方法, 两者都是键值对应,区别是valueforkey 只允许使用NSString类型,objectforkey可以是任意类型.
objectForKey:
is an NSDictionary
method. An NSDictionary
is a collection class similar to an NSArray
, except instead of using indexes, it uses keys to differentiate between items. A key is an arbitrary string you provide. No two objects can have the same key (just as no two objects in an NSArray
can have the same index).
valueForKey:
is a KVC method. It works with ANY class. "valueForKey" allows you to access an instance variable using a string. So for instance, if I have an Account
class with an instance variable accountNumber
, I can do the following:
NSNumber*anAccountNumber =[NSNumber numberWithInt:12345];
Account*newAccount =[[Account alloc] init];
[newAccount setAccountNumber:anAccountNUmber];
NSNumber*anotherAccountNumber =[newAccount accountNumber];
Using KVC, I can also do it like this:
NSNumber*anAccountNumber =[NSNumber numberWithInt:12345];
Account*newAccount =[[Account alloc] init];
[newAccount setValue:anAccountNumber forKey:@"accountNumber"];
NSNumber*anotherAccountNumber =[newAccount valueForKey:@"accountNumber"];
Those are equivalent sets of statements.
I know you're thinking: wow, but sarcastically. KVC doesn't look all that useful. In fact, it looks "wordy". But when you want to change things at runtime, you can do lots of cool things that are much more difficult in other languages (but this is beyond the scope of your question).
If you want to learn more about KVC, there are many tutorials if you Google especially at Scott Stevenson's blog.
Hope that helps.
When you do valueForKey:
you need to give it an NSString, whereas objectForKey:
can take any NSObject subclass as a key. This is because for Key-Value Coding, the keys are always strings.
In fact, the documentation states that even when you give valueForKey:
an NSString, it will invokeobjectForKey:
anyway unless the string starts with an @
, in which case it invokes [super valueForKey:]
, which may call valueForUndefinedKey:
which may raise an exception.此段是说C字符串无效,必须是@开头的字符串
本文转载自:http://www.cnblogs.com/pengyingh/articles/2366182.html
分享到:
相关推荐
- `NSDictionary` 和 `NSMutableDictionary` 使用 `objectForKey:` 获取键对应的值,如:`id value = [dict objectForKey:key];` - `setValue:forKey:` 用于设置字典中键对应的值(仅限 `NSMutableDictionary`)。...
id object = [dict valueForKey:key]; } BOOL keyExists = [dict objectForKey:key] != nil; ``` 以上代码展示了如何初始化字典、访问字典中的值、遍历字典中的键值对以及检查键的存在性。 通过以上的介绍,我们...
NSDictionary *savedItems = [[NSUserDefaults standardUserDefaults] objectForKey:@"sortedItems"]; NSArray *restoredItems = savedItems[@"items"] ? [NSArray arrayWithArray:savedItems[@"items"]] : items; ...
NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSArray *localArray = [localVersion componentsSeparatedByString:@"."];//1.0 NSArray *...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip