Thursday, December 12, 2013

KVC Collection Operators

Itt olvahato a teljes NSHipster iras.

Simple Collection Operators:

  • @count: Returns the number of objects in the collection as an NSNumber.
  • @sum: Converts each object in the collection to a double, computes the sum, and returns the sum as an NSNumber.
  • @avg: Takes the double value of each object in the collection, and returns the average value as an NSNumber.
  • @max: Determines the maximum value using compare:. Objects must support comparison with one another for this to work.
  • @min: Same as @max, but returns the minimum value in the collection.

[products valueForKeyPath:@"@count"]; // 4[products valueForKeyPath:@"@sum.price"]; // 3526.00[products valueForKeyPath:@"@avg.price"]; // 881.50[products valueForKeyPath:@"@max.price"]; // 1699.00[products valueForKeyPath:@"@min.launchedOn"]; // June 11, 2012

Object Operators:
  • @unionOfObjects / @distinctUnionOfObjects: Returns an array of the objects in the property specified in the key path to the right of the operator.
    @distinctUnionOfObjects removes duplicates, whereas @unionOfObjects does not.
Array and Set Operators:
  • @distinctUnionOfArrays / @unionOfArrays: Returns an array containing the combined values of each array in the collection, as specified by the key path to the right of the operator. As you'd expect, the distinct version removes duplicate values.
  • @distinctUnionOfSets: Similar to @distinctUnionOfArrays, but it expects an NSSet containing NSSet objects, and returns an NSSet. Because sets can't contain duplicate values anyway, there is only the distinct operator.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.