IOS開發之——objectForKey與valueForKey在NSDictionary中的差異
来源:互联网 发布:js拼接json字符串 编辑:程序博客网 时间:2024/06/05 16:25
從 NSDictionary 取值的時候有兩個方法,objectForKey: 和 valueForKey:,這兩個方法具體有什麼不同呢?
先從 NSDictionary 文檔中來看這兩個方法的定義:
objectForKey: returns the value associated with aKey, or nil if no value is associated with aKey. 返回指定 key 的 value,若沒有這個 key 返回 nil.
valueForKey: returns the value associated with a given key. 同样是返回指定 key 的 value。
直觀上看這兩個方法好像沒有什麼區別,但文檔裏 valueForKey: 有額外一點:
If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key. via Discussion
一般來說 key 可以是任意字符串組合,如果 key 不是以 @ 符號開頭,這時候 valueForKey: 等同於 objectForKey:,如果是以 @ 開頭,去掉 key 裏的 @ 然後用剩下部分作为 key 執行 [super valueForKey:]。
比如:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue"
forKey:@"theKey"];
NSString *value1 = [dict objectForKey:@"theKey"];
NSString *value2 = [dict valueForKey:@"theKey"];
這時候 value1 和 value2 是一样的結果。如果是這样一個 dict:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue"
forKey:@"@theKey"];// 注意這個 key 是以 @ 開頭
NSString *value1 = [dict objectForKey:@"@theKey"];
NSString *value2 = [dict valueForKey:@"@theKey"];
value1 可以正確取值,但是 value2 取值會直接 crash 掉,報錯信息:
Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<__NSCFDictionary 0x892fd80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key theKey.’
這是因为 valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 裏可以通過 property 同名字符串來獲取對應的值。比如:
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@end
...
Person *person = [[Person alloc] init];
person.name = @"fannheyward";
NSLog(@"name:%@", [person name]);
//name:fannheyward
NSLog(@"name:%@", [person valueForKey:@"name"]);
//name:fannheyward
[person release];
valueForKey: 取值是找和指定 key 同名的 property accessor,沒有的時候執行 valueForUndefinedKey:,而 valueForUndefinedKey: 的默認實現是拋出 NSUndefinedKeyException 異常。
回過頭來看剛才 crash 的例子, [dict valueForKey:@"@theKey"]; 會把 key 裏的 @ 去掉,也就變成了 [dict valueForKey:@"theKey"];,而 dict 不存在 theKey 這样的 property,轉而執行 [dict valueForUndefinedKey:@"theKey"];,拋出 NSUndefinedKeyException 異常後 crash 掉。
objectForKey: 和 valueForKey: 在多數情況下都是一样的結果返回,但是如果 key 是以 @ 開頭,valueForKey: 就成了一個大坑,建議在 NSDictionary 下只用 objectForKey: 來取值。
- IOS開發之——objectForKey與valueForKey在NSDictionary中的差異
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- iOS开发中的objectForKey和valueForKey在NSDictionary中的区别
- ios开放--objectForKey与valueForKey在NSDictionary中的差异
- iOS积累--objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- [android]AIDL详解
- Mybatis Integer类型,值为0被认为是空字符串的解决办法
- C# 多线程
- 程序1.1 重排字符 (rearrange.c)
- PyMongo安装和使用
- IOS開發之——objectForKey與valueForKey在NSDictionary中的差異
- java---正则表达式
- Sublime Text配置文件设置
- 机器学习算法-K最近邻从原理到实现(Python)
- HDU-2082(母函数)
- 诡异的磁盘空间100%报警分析得出df -h与du -sh *的根本性差别
- C++面向对象模型初探
- iOS:hidesBottomBarWhenPushed的正确用法
- 简单工厂模式