valueforKey:,objectForKey:和valueForKeyPath:有什么区别?

我有两个问题:

  1. valueForKey:objectForKey:之间有什么区别? 是一个是NSDictionary的( objectForKey:和其他人是valueforKey:或者是相反的?

  2. 还有什么区别valueForKey:valueForKeyPath: 它是否与Core Data有关?

请帮忙。

valueForKey:是NSKeyValueCoding协议的一部分,因此是键值编码框架的一部分,它允许您在运行时按名称访问类属性。 这就是NIB的加载方式 – 例如,加载与连接相关的属性名称,然后直接按名称设置值。 这与可视化界面devise工具经常在其他语言中工作的方式形成鲜明对比,产生大量隐藏的静态编译代码。

objectForKey:仅在字典上定义,并通过其键查找对象。 NSDictionary是一个存储值和键之间的连接的类。

因此,可以在NSDictionary上使用valueForKey:返回关于字典的元信息,比如它里面的对象的数量,所有键的列表等objectForKey:实际上是用来查看字典的。

在运行时,区别在于objectForKey:是一个完全不透明实现的方法。 valueForKey:显式依赖于随后调用命名的getters和setter。 后者的原因是你可以将键值编码扩展到键值观察,每当特定对象上的特定属性发生变化时,都要求你通知键值观察。 在运行时,这是通过一个方法debugging来实现的,在这个方法中,原来的setter被一个新调用者replace,然后发送所需的消息。 由于所有消息都是dynamic分派的,只需通过修改对象实例中的表来实现即可。

因此,任何符合键值编码的对象(只是意味着以正确的方式声明和实现你的属性,new-ish @ property / @ synthesize语法会自动执行),而不需要对象本身必须实现任何代码。

还有更多的苹果公司使用键值编码来实现各种function,包括CoreData,但并不是专门为其他技术开发的。

valueForKeyPath:类似于valueForKey:除了可以遍历多个对象。 所以你可以有一堆属性的根对象,每个属性是另一个对象与另一堆属性,等等,使用关键path,你可以检索一个值的方式在该数据结构的叶,而不是为自己遍历对象后面的对象。

总之, valueForKey:valueForKeyPath:提供关于对象实例的信息,并与Objective-C运行时的dynamic特性交互。 objectForKey:是一个字典特定的方法,用于执行字典任务。

加法:

一个例子,编码为Itypes,并假定NSDictionary是键值编码兼容:

 NSDictionary *someDictionary; // create someDictionary, populate it, for example (note: we assume photoOfKeys.jpg // definitely exists, not a good idea for production code — if it doesn't we'll get // a nil there and anything after it won't be added to the dictionary as it'll appear // that we terminated the list): someDictionary = @{ @"favouriteGarment": @"hat", @"@allKeys" : [NSImage imageNamed:NSImageNameDotMac], @(2) : NSArray.new }; NSObject *allKeys; // we make no assumptions about which type @allKeys will be, but are going to assume // we can NSLog it, so it needs to be a descendant of NSObject rather than 'id' so as // to definitely respond to the 'description' message — actually this is just compile // time semantics, but if someone else reads this code it'll make it obvious to them // what we were thinking... // some code to get all of the keys stored in the dictionary and print them out; // should print an array containing the strings 'favouriteGarment', '@allKeys' and // the number 2 allKeys = [someDictionary valueForKey:@"@allKeys"]; NSLog(@"%@", allKeys); // some code to get the object named '@allKeys' from the dictionary; will print // a description of the image created by loading photoOfKeys.jpg, above allKeys = [someDictionary objectForKey:@"@allKeys"]; NSLog(@"%@", allKeys); // `objectForKey is analogous to `objectForKeyedSubscript:`, aka allKeys = someDictionary[@"@allKeys"]; 

allKeys是NSDictionary的属性,如此处所述。 我还添加了从NSString allKeys到一些键的照片的映射。 无论是使用键值编码valueForKey:方法还是使用NSDictionary objectForKey: lookup方法,都会指示是读取对象实例的属性,还是向对象实例发送一条消息,要求它执行其唯一作业。

  1. objectForKey:NSDictionary访问与关键字相关联的对象的方法。 valueForKey: NSObject上的一个方法,用于通过访问方法,属性和/或实例variables的名称访问与任何对象关联的任何值。
  2. valueForKeyPath:可以看作是几个对valueForKey:调用的简写。 如果你愿意的话,你可以把它看作xpath的一种。

这两个语句将导致相同的输出:

 // Using nested valueForKey: NSLog(@"%@", [[myObject valueForKey:@"foo"] valueForKey:@"bar"]); // Can be done with a single valueForKeyPath; NSLog(@"%@", [myObject valueForKeyPath:@"foo.bar"]); 

valueForKey:valueForKeyPath:是KVC(键值编码)的一部分。 介绍和深入的文档可以在这里find: http : //developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/

  1. valueForKey:valueAtKeyPath:NSKeyValueCoding非正式协议中定义的方法,两者的默认实现都由根类NSObject

    objectForKey:NSDictionary上的一个方法。

  2. valueForKey:接受一个属性的值,而valueAtKeyPath:接受一个所谓的keypath。 keypath是一个特定属性的句点分隔的path,如@"relationship.property"