本文介绍了valueforKey:,objectForKey:和valueForKeyPath:有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题:

  1. valueForKey:objectForKey:有什么区别?是一个用于NSDictionary s(objectForKey:),另一个用于valueforKey:,还是相反?

  1. What is the difference between valueForKey: and objectForKey:? Is it that one is for NSDictionarys (objectForKey:) and for others it is valueforKey:, or is it the reverse?

valueForKey:valueForKeyPath:之间还有什么区别?它与核心数据有关吗?

Also what is the difference between valueForKey: and valueForKeyPath:? Has it got something to do with Core Data?

请帮助.

推荐答案

valueForKey:是NSKeyValueCoding协议的一部分,因此是键值编码框架的一部分,该框架允许您在运行时按名称访问类属性.例如,这就是NIB的加载方式—加载与连接关联的属性的名称,然后按名称直接设置值.这与可视界面设计工具通常以其他语言工作时会生成大量隐藏的静态编译代码相反.

valueForKey: is part of the NSKeyValueCoding protocol and is therefore part of the key-value coding framework, which allows you to access class properties by name at runtime. That's how NIBs are loaded, for example — the names of properties associated with connections are loaded and then the values are set directly by name. This contrasts with the way that visual interface design tools often work in other languages, generating lots of hidden statically compiled code.

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

objectForKey: is defined on dictionaries only, and looks up an object by its key. An NSDictionary is a class that stores connections between values and keys.

因此,可以在NSDictionary上使用valueForKey:来返回有关字典的元信息,例如其中的对象计数,所有键的列表等.objectForKey:实际上将用于查看字典中的内容.字典.

So, valueForKey: could be used on an NSDictionary to return meta information about the dictionary, such as the count of objects inside it, the list of all keys, etc. objectForKey: would be used actually to look into the dictionary.

在运行时,不同之处在于objectForKey:是具有完全不透明实现的方法. valueForKey:显式依赖于随后调用命名的getter和setter.后者的原因是,您可以将键值编码扩展到键值观察,在此情况下,每次特定对象上的特定属性发生更改时,您都要求通知该值.在运行时,可以通过方法swizzle来实现,在该方法中,原始的设置器将被一个新的替换,该新的调用该设置器,然后发送所需的消息.由于所有消息都是动态分配的,因此只需修改对象实例中的表即可.

At runtime, the difference is that objectForKey: is a method with a completely opaque implementation. valueForKey: explicitly relies on subsequently calling named getters and setters. The reason for the latter is that you can extend key-value coding to key-value observing, where you ask to be informed every time a particular property on a particular object changes. At runtime that's achieved with a method swizzle, where the original setter is replaced by a new one that calls the previous setter and then sends out the required messages. Because all messages are dispatched dynamically, that's just achieved by modifying tables within the object instance.

因此,可以观察到任何符合键值编码的对象(这意味着以正确的方式声明和实现属性,新的@ property/@ synthesize语法会自动执行),而无需观察对象本身实施任何代码.

So any object that is key-value coding compliant (which just means declaring and implementing your properties in the proper way, which the new-ish @property/@synthesize syntax does automatically) can be observed without the object itself having to implement any code.

还有其他Apple产品使用键值编码来实现各种功能,包括CoreData,但并不是专门启用任何其他技术.

There's further Apple stuff that uses key-value coding to achieve various things, including CoreData, but it's not specifically to enable any one other technology.

valueForKeyPath:类似于valueForKey:,但它可以遍历多个对象.因此,您可以拥有一个带有一堆属性的根对象,这些属性中的每一个都是另一个具有一堆属性的对象,等等,并且使用键路径,您可以在该数据结构的叶中检索出一条值的出路,而不必为自己一个接一个地遍历对象.

valueForKeyPath: is like valueForKey: except that it can traverse several objects. So you can have a root object with a bunch of properties, each of those properties is another object with another bunch of properties, etc, and using a key path you can retrieve a value way out at the leaf of that data structure rather than having to iterate through object after object for yourself.

总而言之,valueForKey:valueForKeyPath:提供有关对象实例的信息,并与Objective-C运行时的动态性质进行交互. objectForKey:是执行字典任务的特定于字典的方法.

In summary, valueForKey: and valueForKeyPath: provide information about object instances and interact with the dynamic nature of the Objective-C runtime. objectForKey: is a dictionary specific method that does dictionary tasks.

添加项:

以我输入的代码为例,并假设NSDictionary符合键值编码:

An example, coded as I type and assuming that NSDictionary is key-value coding compliant:

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:查找方法决定了我是读取对象实例的属性还是向对象实例发送一条消息,要求其执行其独特的工作.

allKeys is a property of NSDictionary as described here. I've also added a mapping from the NSString allKeys to a photograph of some keys. Whether I use the key-value coding valueForKey: methods or the NSDictionary objectForKey: lookup method dictates whether I read the property of the object instance or whether I send the object instance a message asking it to do its unique job.

这篇关于valueforKey:,objectForKey:和valueForKeyPath:有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:35