`
webcode
  • 浏览: 5957229 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

What is the difference between objectForKey and valueForKey?

 
阅读更多

Q: What is the difference between objectForKey and valueForKey? I looked both up in the documentation and they seemed the same to me.

--------------------------------------------------------------------------------------------------------------------------------------------------

A: "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 2 objects can have the same key (just as no 2 objects in an NSArray can have the same index).

"valueForKey: is 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:

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.

分享到:
评论

相关推荐

    XCodeGhost

    NULL 博文链接:https://justsee.iteye.com/blog/2252988

    SecureMappingKit.zip

    - (id)objectForKey:(id)aKey expectedClass:(Class)expectedClass withTransformerClass:(Class)transformerClass; - (id)objectForKey:(id)aKey withTransformerBlock:(JMOTransformerBlock)transformerBlock...

    iOS下json解析工具

    NSLog(@"json1.a:%@",[data1 objectForKey"a"]); NSLog(@"json1.b:%@",[data1 objectForKey"b"]); [json1 release]; // 如果json有嵌套,即value里有array、object,如果再使用objectFromJSONString,程序可能会...

    ios-引导页.zip

    int aa=[[defs1 objectForKey:@"ishelp"] intValue]; if (aa!=5) { [defs1 setObject:@"5" forKey:@"ishelp"]; [self helpScroll]; [defs1 synchronize]; } 引导页在程序中只会显示一次 */ ...

    ios开发小技巧

    UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage]; [self performSelector:@selector(selectPic:) withObject:imageafterDelay:0.1]; } -(void)selectPic:(UIImage*)image { NSLog(@...

    爱限免源代码

    NSString * controllerNameStr = [smallDic objectForKey:@"controllerName"]; //将字符串转化为类名 Class controllerNameClass = NSClassFromString(controllerNameStr); //创建类对应的视图控制器对象 ...

    NSDictionary-Accessors:为NSDictionary键入安全访问器,最好与从JSON解析的字典一起使用

    value = [dictionary objectForKey:@"id"];if ([value isKindOfClass:[NSNumber class]]) { model.identifier = [value unsignedLongLongValue];}value = [dictionary objectForKey:@"title"];if ([value ...

    Cocos2d-x 中文字符xml编辑器

    return ((CCString*)strings->objectForKey(key))->m_sString; } static std::string getImagePathsByKey(std::string key) { if(imagePaths == NULL) { std::string path = CCFileUtils::sharedFileUtils()->...

    HBHashButton:通过类别向 UIButtons 添加哈希系统

    HBHashButton 要安装,请将源代码下载到您的项目中。 在您的项目中添加行 #import "UIButton+HBHashButton.h" UIButtons 现在可以像 NSMutableDictionary ...NSLog(@"Value is: %@",[button objectForKey:@"value"])

    plist文件是标准的xml文件

    plist文件是标准的xml文件 A).写入plist文件:(Dictionary/Array) NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] ...NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];

    About SFVerificationCodeView,实现iOS本地生成随机验证码, 从服务器读取并生成验证码 验证码其

    self.codeView.code = [dict objectForKey:@"code"]; }]; }]; [self.codeView didChangeVerificationCode:^(NSString *code) { NSLog(@"self.codeView code:%@",code); }]; //开始生成code [self.codeView ...

    图片预览功能

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self saveImage:image withName:@"currentImage.png"]; NSString *fullPath = [[NSHomeDirectory() ...

    WellCached:一个 NSCache 子类,提供键控下标、基于时间的到期和用于缓存异步数据的类似 Rails 的“获取”API

    [cache objectForKey: @" foo " ]; // nil [cache setObject @" bar " forKey: @" foo " ]; [cache objectForKey: @" foo " ]; // @"bar" 而且,只要您的密钥符合NSCopying协议,您就可以使用类似 NSDictionary 的...

    文字图片结合的滚动视图

    item.itemTitle = [dict objectForKey:@"placeName"]; item.itemDesc = [dict objectForKey:@"placeDesc"]; item.itemImage = [dict objectForKey:@"placeImage"]; [array addObject:item]; } ...

    SYCache:具有磁盘持久性的花式缓存

    - ( void )objectForKey:( NSString *)key usingBlock:( void (^)( id object))block; - ( void )setObject:( id )object forKey:( NSString *)key; 有关方法的完整列表,请参见 。 添加到您的项目 只需将SYCache.h...

    Objective-c对象组装XML

    【原创】转帖请注明来源,谢谢 -tag 1- 测试无内存泄露 1 创建数据对象() NSMutableDictionary *map = [... node.nodeValue = [map objectForKey:key]; [self addChildNode:node]; } } @end

    并行对象缓存TMCache.zip

    // returns immediately[[PINCache sharedCache] objectForKey:@"image" block:^(PINCache *cache, NSString *key, id object) { UIImage *image = (UIImage *)object; NSLog(@"image scale: %f", image.scale);...

    并行对象缓存PINCache.zip

    // returns immediately[[PINCache sharedCache] objectForKey:@"image" block:^(PINCache *cache, NSString *key, id object) { UIImage *image = (UIImage *)object; NSLog(@"image scale: %f", image.scale);...

    Xcode中Info.plist字段详解

    Info.plist用于向iOS提供关于app,bundle或者framework的一些重要信息。它指定了比如一个应用应该怎样启动,它如何被本地化,应用的名称,要显示的图标,...NSString *localVersion = [infoDic objectForKey:@”CFBundl

    ios WKWebView 的使用和交互

    [queryItemArray addObject:[NSURLQueryItem queryItemWithName:@"version" value:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]]; [urlComponents setQueryItems:...

Global site tag (gtag.js) - Google Analytics