valueforkey和objectforkey
2012-04-14 13:33
288 查看
objectforkey 是NSDictionary的方法,valueforkey 是KVC的方法, 两者都是键值对应,区别是valueforkey 只允许使用NSString类型,objectforkey可以是任意类型.
string you provide. No two objects can have the same key (just as no two objects in an
do the following:
Using KVC, I can also do it like this:
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.
objectForKey: accepts any object as a key, not just strings. The only requirement is that the key support the NSCopying protocol.
When you do
In fact, the documentation states that even when you give
which may call
objectForKey:is an
NSDictionarymethod. An
NSDictionaryis 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
NSArraycan 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
Accountclass 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.
objectForKey: accepts any object as a key, not just strings. The only requirement is that the key support the NSCopying protocol.
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 invoke
objectForKey:anyway unless the string starts with an
@, in which case it invokes
[super valueForKey:],
which may call
valueForUndefinedKey:which may raise an exception.此段是说C字符串无效,必须是@开头的字符串
相关文章推荐
- iOS 中 objectForKey与valueForKey 的区别
- objectForKey与valueForKey在NSDictionary中的差异
- iOS学习之——objectForKey:和valueForKey:的不同
- 【iOS】Difference Between objectForKey and valueForKey in NSDictionary
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- valueforkey objectforkey区别,
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- KVO 接收变更通知 observeValueForKeyPath:ofObject:change:context:
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- POST请求报错:Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around charact
- ios开放--objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- objectForKey与valueForKey在NSDictionary中的差异
- NSDictionary之objectForKey 和 valueForKey 的不同
- iOS开发中setObject:ForKey和setValue:ForKey的用法和区别
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: WebKitLocalStorageDatabasePathPreferenc