小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

IOS開發(fā)之——objectForKey與valueForKey在

 quasiceo 2015-04-03
分類: iOS開發(fā)技術(shù)系列專欄 2012-05-16 13:17 8079人閱讀 評(píng)論(1) 收藏 舉報(bào)

從 NSDictionary 取值的時(shí)候有兩個(gè)方法,objectForKey: 和 valueForKey:,這兩個(gè)方法具體有什么不同呢?

先從 NSDictionary 文檔中來看這兩個(gè)方法的定義:

objectForKey: returns the value associated with aKey, or nil if no value is associated with aKey. 返回指定 key 的 value,若沒有這個(gè) key 返回 nil.

valueForKey: returns the value associated with a given key. 同樣是返回指定 key 的 value。
 

直觀上看這兩個(gè)方法好像沒有什么區(qū)別,但文檔里 valueForKey: 有額外一點(diǎn):

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 不是以 @ 符號(hào)開頭,這時(shí)候 valueForKey: 等同于 objectForKey:,如果是以 @ 開頭,去掉 key 里的 @ 然后用剩下部分作為 key 執(zhí)行 [super valueForKey:]。
 

比如:

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue"

                                                 forKey:@"theKey"];

NSString *value1 = [dict objectForKey:@"theKey"];

NSString *value2 = [dict valueForKey:@"theKey"];
 

這時(shí)候 value1 和 value2 是一樣的結(jié)果。如果是這樣一個(gè) dict:

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue"

                                                 forKey:@"@theKey"];// 注意這個(gè) key 是以 @ 開頭

NSString *value1 = [dict objectForKey:@"@theKey"];

NSString *value2 = [dict valueForKey:@"@theKey"];
 

value1 可以正確取值,但是 value2 取值會(huì)直接 crash 掉,報(bào)錯(cuò)信息:

Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<__NSCFDictionary 0x892fd80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key theKey.’


這是因?yàn)?valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 里可以通過 property 同名字符串來獲取對(duì)應(yīng)的值。比如:

@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,沒有的時(shí)候執(zhí)行 valueForUndefinedKey:,而 valueForUndefinedKey: 的默認(rèn)實(shí)現(xiàn)是拋出 NSUndefinedKeyException 異常。

回過頭來看剛才 crash 的例子, [dict valueForKey:@"@theKey"]; 會(huì)把 key 里的 @ 去掉,也就變成了 [dict valueForKey:@"theKey"];,而 dict 不存在 theKey 這樣的 property,轉(zhuǎn)而執(zhí)行 [dict valueForUndefinedKey:@"theKey"];,拋出 NSUndefinedKeyException 異常后 crash 掉。

objectForKey: 和 valueForKey: 在多數(shù)情況下都是一樣的結(jié)果返回,但是如果 key 是以 @ 開頭,valueForKey: 就成了一個(gè)大坑,建議在 NSDictionary 下只用 objectForKey: 來取值。

3
 
ios開發(fā) interface excepti準(zhǔn)備好了么? 
成都匯資聚
發(fā)表評(píng)論
  • 用 戶 名:
  • ilvu999
  • 評(píng)論內(nèi)容:
  • 插入代碼

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多