KVC官方文档学习(三)----KVC基础之访问集合属性

官方文档

Key-value coding compliant objects expose their to-many properties in the same way that they expose other properties. You can get or set a collection object just as you would any other object using valueForKey: and setValue:forKey: (or their key path equivalents). However, when you want to manipulate the content of these collections, it’s usually most efficient to use the mutable proxy methods defined by the protocol.

符合KVC的对象以与他们公开其他属性相同的方式公开他们的to-many属性。你可以使用valueForKey:setValue:forKey:来获取或设置一个集合对象,就像使用任何其他对象一样。然而,当你想要操纵这些集合的内容时,通常使用协议定义的可变代理方法时更有效的。

The protocol defines three different proxy methods for collection object access, each with a key and a key path variant:

该协议为集合对象访问定义了三个不同的代理方法,每种方法有一个key和key path变种:

  • mutableArrayValueForKey: and mutableArrayValueForKeyPath:
    These return a proxy object that behaves like an NSMutableArray object.
  • mutableArrayValueForKey: 和 mutableArrayValueForKeyPath:
    这些返回一个表现得像一个NSMutableArray对象的代理对象。
  • mutableSetValueForKey: and mutableSetValueForKeyPath:
    These return a proxy object that behaves like an NSMutableSet object.
  • mutableSetValueForKey: 和 mutableSetValueForKeyPath:
    这些返回一个表现得像NSMutableSet对象的代理对象。
  • mutableOrderedSetValueForKey: and mutableOrderedSetValueForKeyPath:
    These return a proxy object that behaves like an NSMutableOrderedSet object.
  • mutableOrderedSetValueForKey: 和 mutableOrderedSetValueForKeyPath:
    这些返回一个表现得像NSMutableOrderedSet对象的代理对象。

When you operate on the proxy object, adding objects to, removing objects from, or replacing objects in it, the default implementation of the protocol modifies the underlying property accordingly. This is more efficient than obtaining a non-mutable collection object with valueForKey:, creating a modified one with altered content, and then storing it back to the object with a setValue:forKey: message. In many cases, it is also more efficient than working directly with a mutable property. These methods provide the additional benefit of maintaining key-value observing compliance for the objects held in the collection object (see Key-Value Observing Programming Guide for details.

当你操作代理对象,向其添加对象,从中删除对象或替换对象时,协议的默认实现会相应地修改基础属性。这比使用valueForKey:获取一个非可变集合对象更有效,创建具有更改内容的已修改集合对象,然后使用setValue:forKey:消息将其存储回对象。在很多情况下,它比直接使用可变属性更有效。这些方法提供了维护集合对象中保存的对象的键值观察遵从性的额外好处(有关详细信息,请参阅Key-Value Observing Programming Guide)。

你可能感兴趣的:(KVC官方文档学习(三)----KVC基础之访问集合属性)