KVC官方文档学习(四)----KVC基础之使用集合运算符

官方文档

When you send a key-value coding compliant object the valueForKeyPath: message, you can embed a collection operator in the key path. A collection operator is one of a small list of keywords preceded by an at sign (@) that specifies an operation that the getter should perform to manipulate the data in some way before returning it. The default implementation of valueForKeyPath: provided by NSObject implements this behavior.

当给一个符合KVC的对象发送 valueForKeyPath: 消息时,可以在键路径中嵌入一个集合运算符。集合运算符是一个小的关键字列表之一,前面是一个at符号(@),它指定了getter应该执行的操作,以便在返回之前以某种方式操作数据。 NSObject提供的 valueForKeyPath: 的默认实现实现了这个行文。

When a key path contains a collection operator, any portion of the key path preceding the operator, known as the left key path, indicates the collection on which to operate relative to the receiver of the message. If you send the message directly to a collection object, such as an NSArray instance, the left key path may be omitted.

当键路径包含一个集合运算符时,运算符之前的键路径的任何部分(称为左键路径)表示相对于消息接收者的集合。 如果直接给集合对象发消息(例如一个NSArray实例)则可以省略左键路径。

The portion of the key path after the operator, known as the right key path, specifies the property within the collection that the operator should work on. All the collection operators except @count require a right key path. Figure 4-1 illustrates the operator key path format.

运算符后面的键路径部分(称为右键路径),指示运算符应处理的集合中的属性。除了@count之外的所有运算符都需要一个右键路径。图4-1说明了运算符键路径格式。

4-1 运算符键路径格式

Collection operators exhibit three basic types of behavior:

集合运算符展示出三种基本表现类型:

  • Aggregation Operators coalesce a collection’s objects in some way, and return a single object that generally matches the data type of the property named in the right key path. The @count operator is an exception—it takes no right key path and always returns an NSNumber instance.
  • 聚合运算符 以某种方式合并集合的对象,并返回通常与右键路径中指定的属性的数据类型匹配的单个对象。@count运算符时一个例外,它没有右键路径并总是返回一个NSNumber实例。
  • Array Operators return an NSArray instance containing some subset of the objects held in the named collection.
  • 数组运算符 返回一个NSArray 实例,该实例包含命名集合中保存的对象的一些子集。
  • Nesting Operators work on collections that contain other collections, and return an NSArray or NSSetinstance, depending on the operator, that combines the objects of the nested collections in some way.
  • 嵌套运算符 处理包含其他集合的集合,并根据运算符返回一个NSArray或NSSet实例,它以某种方式组合嵌套集合的对象。
样本数据(Sample Data)

The descriptions that follow include code snippets demonstrating how you invoke each operator, and the result of doing so. These rely on the BankAccount class, presented in Listing 2-1, which holds an array of Transaction objects. Each of these represents a simple checkbook entry, as declared in Listing 4-1.

以下描述包括演示如何调用每个运算符的代码片段,以及执行此操作的结果。这些依赖于清单2-1中所示的类BankAccount,它拥有Transaction对象的数组。其中每个代表一个简单的支票簿条目,如Listing 4-1所示。

Listing 4-1 Transaction对象的接口声明

@interface Transaction : NSObject
 
@property (nonatomic) NSString* payee;   // To whom
@property (nonatomic) NSNumber* amount;  // How much
@property (nonatomic) NSDate* date;      // When
 
@end

For the sake of discussion, assume your BankAccount instance has a transactions array populated with the data shown in Table 4-1, and that you make the example calls from inside the BankAccount object.

为了便于讨论,假设您的BankAccount实例有一个使用表4-1中显示的数据填充的transactions数组,并且您可以从BankAccount对象内部进行示例调用。

表4-1 Transactions对象的示例数据

payee 值 amount 格式为货币的值 date 格式为月,日的值
Green Power $120.00 Dec 1, 2015
Green Power $150.00 Jan 1, 2016
Green Power $170.00 Feb 1, 2016
Car Loan $250.00 Jan 15, 2016
Car Loan $250.00 Feb 15, 2016
Car Loan $250.00 Mar 15, 2016
General Cable $120.00 Dec 1, 2015
General Cable $155.00 Jan 1, 2016
General Cable $120.00 Feb 1, 2016
Mortgage $1,250.00 Jan 15, 2016
Mortgage $1,250.00 Feb 15, 2016
Mortgage $1,250.00 Mar 15, 2016
Animal Hospital $600.00 Jul 15, 2016
聚合运算符(Aggregation Operators)

Aggregation operators work on either an array or set of properties, producing a single value that reflects some aspect of the collection.

聚合运算符可以处理数组或属性集,从而生成反映集合某些方面的单个值。

@avg

When you specify the @avg operator, valueForKeyPath: reads the property specified by the right key path for each element of the collection, converts it to a double (substituting 0 for nil values), and computes the arithmetic average of these. It then returns the result stored in an NSNumber instance.

指定@avg运算符时,valueForKeyPath:读取集合中每个元素的右键路径指定的属性,将其转换为double(用0代替nil值),并计算这些值的算术平均值。然后它返回存储在NSNumber实例中的结果。

要获取表4-1中的样本数据之间的平均交易金额:

NSNumber *transactionAverage = [self.transactions valueForKeyPath:@"@avg.amount"];

transactionAverage的格式化结果为$ 456.54。

@count

When you specify the @count operator, valueForKeyPath: returns the number of objects in the collection in an NSNumber instance. The right key path, if present, is ignored.

指定@count运算符时,valueForKeyPath:返回NSNumber实例中集合中的对象数。右键路径(如果存在)将被忽略。

获取Transaction对象的数量transactions:

NSNumber *numberOfTransactions = [self.transactions valueForKeyPath:@"@count"];

值numberOfTransactions是13。

@max

When you specify the @max operator, valueForKeyPath: searches among the collection entries named by the right key path and returns the largest one. The search conducts comparisons using the compare: method, as defined by many Foundation classes, such as the NSNumber class. Therefore, the property indicated by the right key path must hold an object that responds meaningfully to this message. The search ignores nil valued collection entries.

指定@max运算符时,valueForKeyPath:在右键路径指定的集合条目中搜索并返回最大的条目。搜索使用compare:方法进行比较,该方法由许多Foundation类(例如NSNumber类)定义。因此,右键路径指示的属性必须包含一个对此消息有意义响应的对象。搜索忽略nil赋值的集合条目。

要获得表4-1中列出的事务中日期值的最大值,即最近事务的日期:

NSDate *latestDate = [self.transactions valueForKeyPath:@"@max.date"];

格式化的latestDate值是2016年7月15日。

@min

When you specify the @min operator, valueForKeyPath:searches among the collection entries named by the right key path and returns the smallest one. The search conducts comparisons using the compare:method, as defined by many Foundation classes, such as the NSNumber class. Therefore, the property indicated by the right key path must hold an object that responds meaningfully to this message. The search ignores nil valued collection entries.

指定@min运算符时,valueForKeyPath:在右键路径指定的集合条目中搜索并返回最小的条目。搜索使用compare:方法进行比较,该方法由许多Foundation类(例如NSNumber类)定义。因此,右键路径指示的属性必须包含一个对此消息有意义响应的对象。搜索忽略nil赋值的集合条目。

要获得表4-1中列出的事务中最小的日期值(即最早的事务的日期):

NSDate *earliestDate = [self.transactions valueForKeyPath:@"@min.date"];

格式化的earliestDate值是2015年12月1日。

@sum

When you specify the @sum operator, valueForKeyPath: reads the property specified by the right key path for each element of the collection, converts it to a double (substituting 0 for nil values), and computes the sum of these. It then returns the result stored in an NSNumber instance.

指定@sum运算符时,valueForKeyPath:读取集合中每个元素的右键路径指定的属性,将其转换为double(将0替换为nil值),并计算这些值的总和。然后它返回存储在NSNumber实例中的结果。

要获取表4-1中样本数据之间的事务数量总和:

NSNumber *amountSum = [self.transactions valueForKeyPath:@"@sum.amount"];

格式化结果amountSum为$ 5,935.00。
数组运算符(Array Operators)

The array operators cause valueForKeyPath: to return an array of objects corresponding to a particular set of the objects indicated by the right key path.

数组运算符导致valueForKeyPath:返回与右键路径指示的特定对象集相对应的对象数组。

重要
在使用数组运算符时如果任何leaf对象nil, valueForKeyPath:方法会引发异常。

@distinctUnionOfObjects

When you specify the @distinctUnionOfObjects operator, valueForKeyPath: creates and returns an array containing the distinct objects of the collection corresponding to the property specified by the right key path.

指定@distinctUnionOfObjects运算符时,valueForKeyPath:创建并返回一个数组,该数组包含与右键路径指定的属性对应的集合的不同对象。

要获取transactions 中的transactions 的payee属性值集合,并且忽略重复值:

NSArray *distinctPayees = [self.transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"];

结果distinctPayees数组包含以下每个字符串的一个实例:Car Loan, General Cable, Animal Hospital, Green Power, Mortgage.

注意
@unionOfObjects运算符提供类似的行为,但没有删除重复的对象。

@unionOfObjects

When you specify the @unionOfObjects operator, valueForKeyPath: creates and returns an array containing all the objects of the collection corresponding to property specified by the right key path. Unlike @distinctUnionOfObjects, duplicate objects are not removed.

指定@unionOfObjects运算符时,valueForKeyPath:创建并返回一个数组,该数组包含与右键路径指定的属性对应的集合的所有对象。与@distinctUnionOfObjects不同,不会移除重复的对象。

要获取transactions中transactions 的payee属性值集合:

NSArray *payees = [self.transactions valueForKeyPath:@"@unionOfObjects.payee"];

结果payees阵列包含以下字符串:Green Power, Green Power, Green Power, Car Loan, Car Loan, Car Loan, General Cable, General Cable, General Cable, Mortgage, Mortgage, Mortgage, Animal Hospital。请注意重复项。

注意
@distinctUnionOfArrays运算符类似,但移除了重复的对象。

嵌套运算符(Nesting Operators)

嵌套运算符在嵌套集合上运行,集合的每个条目本身都包含一个集合。

重要
在使用嵌套运算符时如果任何叶对象为nilvalueForKeyPath:方法会引发异常。

对于下面的描述,请考虑第二个数据数组moreTransactions,使用表4-2中的数据填充,并与原始transactions数组(从“ 示例数据”部分)一起收集到嵌套数组中:

NSArray* moreTransactions = @[<# transaction data #>];
NSArray* arrayOfArrays = @[self.transactions, moreTransactions];

表4-2假设moreTransactions数组中的Transaction数据

payee 值 amount 格式为货币的值 date 格式为月,日的值
General Cable - Cottage $120.00 Dec 18, 2015
General Cable - Cottage $155.00 Jan 9, 2016
General Cable - Cottage $120.00 Dec 1, 2016
Second Mortgage $1,250.00 Nov 15, 2016
Second Mortgage $1,250.00 Sep 20, 2016
Second Mortgage $1,250.00 Feb 12, 2016
Hobby Shop $600.00 Jun 14, 2016

@distinctUnionOfArrays

When you specify the @distinctUnionOfArrays operator, valueForKeyPath:creates and returns an array containing the distinct objects of the combination of all the collections corresponding to the property specified by the right key path.

指定@distinctUnionOfArrays运算符时,valueForKeyPath:创建并返回一个数组,该数组包含与右键路径指定的属性对应的所有集合的组合的不同对象。

要在arrayOfArrays:所有数组中获取payee属性的不同值:

NSArray *collectedDistinctPayees = [arrayOfArrays valueForKeyPath:@"@distinctUnionOfArrays.payee"];

结果collectedDistinctPayees数组包含以下值:Hobby Shop, Mortgage, Animal Hospital, Second Mortgage, Car Loan, General Cable - Cottage, General Cable, Green Power.

注意
@unionOfArrays操作符是类似的,但不删除重复的对象。

@unionOfArrays

When you specify the @unionOfArrays operator,valueForKeyPath:creates and returns an array containing the all the objects of the combination of all the collections corresponding to the property specified by the right key path, without removing duplicates.

指定@unionOfArrays运算符时,valueForKeyPath:创建并返回一个数组,该数组包含与右键路径指定的属性对应的所有集合的组合的所有对象,而不删除重复项。

要获取`arrayOfArrays:`中所有数组中的`payee`属性值:

NSArray *collectedPayees = [arrayOfArrays valueForKeyPath:@"@unionOfArrays.payee"];

结果的collectedPayees数组包含以下值:Green Power, Green Power, Green Power, Car Loan, Car Loan, Car Loan, General Cable, General Cable, General Cable, Mortgage, Mortgage, Mortgage, Animal Hospital, General Cable - Cottage, General Cable - Cottage, General Cable - Cottage, Second Mortgage, Second Mortgage, Second Mortgage, Hobby Shop.

注意
@distinctUnionOfArrays运算符类似,但删除了重复的对象。

@distinctUnionOfSets

When you specify the @distinctUnionOfSets operator, valueForKeyPath:creates and returns an NSSet object containing the distinct objects of the combination of all the collections corresponding to the property specified by the right key path.

指定@distinctUnionOfSets运算符时,valueForKeyPath:创建并返回一个NSSet对象,该对象包含与右键路径指定的属性对应的所有集合的组合的不同对象。

This operator behaves just like @distinctUnionOfArrays, except that it expects an NSSet instance containing NSSet instances of objects rather than an NSArray instance of NSArray instances. Also, it returns an NSSet instance. Assuming the example data had been stored in sets instead of arrays, the example call and results are the same as those shown for @distinctUnionOfArrays.

此运算符的行为与此@distinctUnionOfArrays类似,只是它需要一个NSSet包含NSSet对象实例而不是包含NSArray实例的NSArray实例。此外,它返回一个NSSet实例。假设示例数据已存储在集合而不是数组中,示例调用和结果与显示的相同@distinctUnionOfArrays

你可能感兴趣的:(KVC官方文档学习(四)----KVC基础之使用集合运算符)