Objective-C:forKey和forKeyPath有什么区别?

时间:2021-04-16 22:30:15

What is the difference between forKey and forKeyPath in NSMutableDicitionary's setValue method? I looked both up in the documentation and they seemed the same to me. I tried following code, but I couldn't distinguish the difference.

在NSMutableDicitionary的setValue方法中,forKey和forKeyPath有什么区别?我在文档中查看了它们,它们对我来说似乎是一样的。我尝试了下面的代码,但我无法区分它们。

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@"abc" forKey:@"ABC"];
[dict setValue:@"123" forKeyPath:@"xyz"];
for (NSString* key in dict) {
    id val = [dict objectForKey:key];
    NSLog(@"%@, %@", key, val);
}

3 个解决方案

#1


7  

Both methods are part of Key-Value Coding and should not generally be used for element access in dictionaries. They only work on keys of type NSString and require specific syntax.

这两种方法都是键值编码的一部分,通常不应用于字典中的元素访问。它们只能处理NSString类型的键,并且需要特定的语法。

The difference between both of them is that specifying a (single) key would just look up that item.

它们之间的区别在于指定(单个)键只会查找该项。

Specifying a key path on the other hand follows the path through the objects. If you had a dictionary of dictionaries you could look up an element in the second level by using a key path like "key1.key2".

另一方面,指定键路径遵循通过对象的路径。如果您有字典词典,则可以使用“key1.key2”之类的键路径在第二级中查找元素。

If you just want to access elements in a dictionary you should use objectForKey: and setObject:forKey:.

如果您只想访问字典中的元素,则应使用objectForKey:和setObject:forKey:。

Edit to answer why valueForKey: should not be used:

编辑以回答为什么valueForKey:不应该使用:

  1. valueForKey: works only for string keys. Dictionaries can use other objects for keys.
  2. valueForKey:仅适用于字符串键。字典可以使用其他对象作为键。
  3. valueForKey: Handles keys that start with an "@" character differently. You cannot access elements whose keys start with @.
  4. valueForKey:以不同方式处理以“@”字符开头的键。您无法访问其键以@开头的元素。
  5. Most importantly: By using valueForKey: you are saying: "I'm using KVC". There should be a reason for doing this. When using objectForKey: you are just accessing elements in a dictionary through the natural, intended API.
  6. 最重要的是:通过使用valueForKey:你说:“我正在使用KVC”。应该有这样做的理由。使用objectForKey时:您只是通过自然的API访问字典中的元素。

#2


3  

Apple Documentation: NSKeyValueCoding Protocol Reference

Apple文档:NSKeyValueCoding协议参考

setValue:forKey:

的setValue:forKey:

Sets the property of the receiver specified by a given key to a given value.

将给定键指定的接收器的属性设置为给定值。

Example:

例:

[foo setValue:@"blah blah" forKey:@"stringOnFoo"];

setValue:forKeyPath:

setValue方法:forKeyPath:

Sets the value for the property identified by a given key path to a given value.

将给定键路径标识的属性的值设置为给定值。

Example:

例:

[foo setValue:@"The quick brown fox" forKeyPath:@"bar.stringOnBar"];

#3


1  

keyPath can be used to traverse through arbitrary object paths, as long as they implement NSKeyValueCoding. They don't need to be NSDictionarys. For example:

keyPath可用于遍历任意对象路径,只要它们实现NSKeyValueCoding即可。他们不需要是NSDictionarys。例如:

NSString *departmentName = [self valueForKeyPath:@"[person.department.name"];

Although this is a bit of a contrived example.

虽然这是一个人为的例子。

#1


7  

Both methods are part of Key-Value Coding and should not generally be used for element access in dictionaries. They only work on keys of type NSString and require specific syntax.

这两种方法都是键值编码的一部分,通常不应用于字典中的元素访问。它们只能处理NSString类型的键,并且需要特定的语法。

The difference between both of them is that specifying a (single) key would just look up that item.

它们之间的区别在于指定(单个)键只会查找该项。

Specifying a key path on the other hand follows the path through the objects. If you had a dictionary of dictionaries you could look up an element in the second level by using a key path like "key1.key2".

另一方面,指定键路径遵循通过对象的路径。如果您有字典词典,则可以使用“key1.key2”之类的键路径在第二级中查找元素。

If you just want to access elements in a dictionary you should use objectForKey: and setObject:forKey:.

如果您只想访问字典中的元素,则应使用objectForKey:和setObject:forKey:。

Edit to answer why valueForKey: should not be used:

编辑以回答为什么valueForKey:不应该使用:

  1. valueForKey: works only for string keys. Dictionaries can use other objects for keys.
  2. valueForKey:仅适用于字符串键。字典可以使用其他对象作为键。
  3. valueForKey: Handles keys that start with an "@" character differently. You cannot access elements whose keys start with @.
  4. valueForKey:以不同方式处理以“@”字符开头的键。您无法访问其键以@开头的元素。
  5. Most importantly: By using valueForKey: you are saying: "I'm using KVC". There should be a reason for doing this. When using objectForKey: you are just accessing elements in a dictionary through the natural, intended API.
  6. 最重要的是:通过使用valueForKey:你说:“我正在使用KVC”。应该有这样做的理由。使用objectForKey时:您只是通过自然的API访问字典中的元素。

#2


3  

Apple Documentation: NSKeyValueCoding Protocol Reference

Apple文档:NSKeyValueCoding协议参考

setValue:forKey:

的setValue:forKey:

Sets the property of the receiver specified by a given key to a given value.

将给定键指定的接收器的属性设置为给定值。

Example:

例:

[foo setValue:@"blah blah" forKey:@"stringOnFoo"];

setValue:forKeyPath:

setValue方法:forKeyPath:

Sets the value for the property identified by a given key path to a given value.

将给定键路径标识的属性的值设置为给定值。

Example:

例:

[foo setValue:@"The quick brown fox" forKeyPath:@"bar.stringOnBar"];

#3


1  

keyPath can be used to traverse through arbitrary object paths, as long as they implement NSKeyValueCoding. They don't need to be NSDictionarys. For example:

keyPath可用于遍历任意对象路径,只要它们实现NSKeyValueCoding即可。他们不需要是NSDictionarys。例如:

NSString *departmentName = [self valueForKeyPath:@"[person.department.name"];

Although this is a bit of a contrived example.

虽然这是一个人为的例子。