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
分享到:
相关推荐
NULL 博文链接:https://justsee.iteye.com/blog/2252988
SecureMappingKit 可以实现 JSON 数据和 Model 类之间安全的映射。 转换器包括: NSNumberTransformer, NSBooleanNumberTransformer, to be sure to have a NSNumber of a boolean NSStringTransformer, ...
NSLog(@"json1.a:%@",[data1 objectForKey"a"]); NSLog(@"json1.b:%@",[data1 objectForKey"b"]); [json1 release]; // 如果json有嵌套,即value里有array、object,如果再使用objectFromJSONString,程序可能会...
NSString * controllerNameStr = [smallDic objectForKey:@"controllerName"]; //将字符串转化为类名 Class controllerNameClass = NSClassFromString(controllerNameStr); //创建类对应的视图控制器对象 ...
return ((CCString*)strings->objectForKey(key))->m_sString; } static std::string getImagePathsByKey(std::string key) { if(imagePaths == NULL) { std::string path = CCFileUtils::sharedFileUtils()->...
value = [dictionary objectForKey:@"id"];if ([value isKindOfClass:[NSNumber class]]) { model.identifier = [value unsignedLongLongValue];}value = [dictionary objectForKey:@"title"];if ([value ...
WellCached 是一个 NSCache 子类,它提供键控下标、基于时间的键过期和一个类似于 Rails 的fetch API,它可以处理来自异步源的数据。 用法 WellCached包括单个类ELAWellCached可以精确地使用像NSCache : ...
plist文件是标准的xml文件 A).写入plist文件:(Dictionary/Array) NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] ...NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];
简单的内存和磁盘缓存。 它由内存中的支持,因此在内存不足时会自动清除自身。 下次请求时,清除的存储密钥将自动从磁盘加载。 用法 该API很简单。 - ( id )objectForKey:( NSString *)key; - ( void )objectForKey...
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() ...
本demo是WKWebView的基本使用和交互 ,实现了原生调用js的方法、js调用原生的方法、通过拦截进行交互的方法;修改内容 加入沙盒 / /加载沙盒 不带参数 // NSArray * paths = NSSearchPathForDirectoriesInDomains...
TMCache 是 Tumblr 公司开发的一个快速,无死锁的并行对象缓存,支持 iOS 和 OS X 系统。示例代码:UIImage *img = [[UIImage alloc] initWithData:data scale:[[UIScreen mainScreen] scale]];[[PINCache ...
PINCache 是 Pinterest 公司开发的一个快速,无死锁的并行对象缓存,支持 iOS 和 OS X 系统, 是 TMCache 的改进版本。示例代码:UIImage *img = [[UIImage alloc] initWithData:data scale:[[UIScreen mainScreen] ...
item.itemTitle = [dict objectForKey:@"placeName"]; item.itemDesc = [dict objectForKey:@"placeDesc"]; item.itemImage = [dict objectForKey:@"placeImage"]; [array addObject:item]; } ...
一个简单的小例子,通过一个xml文件去实现选择省市区。 关键是用了XML解析器的代理方法。具体相关代码如下: #pragma mark - xmlParseDelegateMethod ... tempProvince = [attributeDict objectForKey:@"name"];
HBHashButton 要安装,请将源代码下载到您的项目中。 在您的项目中添加行 #import "UIButton+HBHashButton.h" UIButtons 现在可以像 NSMutableDictionary ...NSLog(@"Value is: %@",[button objectForKey:@"value"])
int aa=[[defs1 objectForKey:@"ishelp"] intValue]; if (aa!=5) { [defs1 setObject:@"5" forKey:@"ishelp"]; [self helpScroll]; [defs1 synchronize]; } 引导页在程序中只会显示一次 */ ...
#方法:读取数据:{[[NSUserDefaults standardUserDefaults] objectForKey:@"keyString"];存储数据:{[[NSUserDefaults standardUserDefaults] setObject:"valve" forKey:@"keyString"]; [[NSUserDefaults ...
Info.plist用于向iOS提供关于app,bundle或者framework的一些重要信息。它指定了比如一个应用应该怎样启动,它如何被本地化,应用的名称,要显示的图标,...NSString *localVersion = [infoDic objectForKey:@”CFBundl