美文网首页Key Value Coding
KVC---使用集合运算

KVC---使用集合运算

作者: 轻云绿原 | 来源:发表于2017-04-14 16:17 被阅读46次

    来源

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/CollectionOperators.html#//apple_ref/doc/uid/20002176-BAJEAIEE

    当你要发送valueForKeyPath:消息给KVC对象时,你可以把集合运算包含在key path里.集合运算符是前面带有(@)的关键字.它可以在数据返回前执行特定的操作.NSObject提供的valueForKeyPath:默认实现实现了这种行为.
    当key path包含集合运算符时,在运算符前面的是left key path,它表示接收运算符的集合.如果你直接把消息发送给集合对象(比如:NSArray对象),left key path可能会被忽略.
    运算符后面的,就是right key path,它表示运算符是在哪个集合属性上发生作用.所有的集合运算符(除了@count外),都需要一个right key path.

    key path 运算格式key path 运算格式

    集合运算的三种基本运算类型:

    • 汇总运算:以某种方式合并集合对象,并返回一个单一的对象,这个对象的类型是和right key path里的属性一样的.@count运算符是特例,你没有right key path并总是返回NSNumber实例.
    • 数组运算:总是返回集合里的子集
    • 嵌套运算:作用在包含了其它集合的集合,并返回一个NSArrayNSSet实例.以某种方式合并了嵌套集合.

    例子

    @interface BankAccount : NSObject
     
    @property (nonatomic) NSNumber* currentBalance;              // An attribute
    @property (nonatomic) Person* owner;                         // A to-one relation
    @property (nonatomic) NSArray< Transaction* >* transactions; // A to-many relation
     
    @end
    
    @interface Transaction : NSObject
     
    @property (nonatomic) NSString* payee;   // To whom
    @property (nonatomic) NSNumber* amount;  // How much
    @property (nonatomic) NSDate* date;      // When
     
    @end
    

    为了讨论,假设BankAccount实例,有以下数据在transactions数组里:

    payee (values) amount (values formatted as currency) date (values formatted as month day, year)
    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

    你可以简单的通过把 self 作为操作符后面的 key path 来获取一个由 NSNumber 组成的数组或者集合的总值,例如对于数组 @[@(1), @(2), @(3)] 可使用 valueForKeyPath:@"@max.self" 来获取最大值。

    @avg:

    valueForKeyPath:读取集合里每个元素的right key path属性,把它转成double(nil值用0表示),并计算它们的算术平均值.最后把结果存在NSNumber实例里并返回.

    NSNumber *transactionAverage = [self.transactions valueForKeyPath:@"@avg.amount"];
    ///transactionAverage 的结果是$456.54
    

    @count

    valueForKeyPath:返回集合里有多少个对象,并存在NSNumber实例里.如果出现right key path将被忽略.

    NSNumber *numberOfTransactions = [self.transactions valueForKeyPath:@"@count"];
    //numberOfTransactions的值为13
    

    @max

    valueForKeyPath:将搜索集合里全部的right key path的实体,并返回最大的那个.这个搜索是用compare:方法来对比的.所以right key path所指向的实体必须是可以使用这个方法的.搜索会忽略nil.

    NSDate *latestDate = [self.transactions valueForKeyPath:@"@max.date"];
    //latestDate的值是 Jul 15, 2016
    

    @min

    与@max相反

    NSDate *earliestDate = [self.transactions valueForKeyPath:@"@min.date"];
    // earliestDate的值是 Dec 1, 2015
    

    @sum

    valueForKeyPath:读取集合里每个元素的right key path属性,把它转成double(nil值用0表示),并计算它们的合.最后把结果存在NSNumber实例里并返回.

    NSNumber *amountSum = [self.transactions valueForKeyPath:@"@sum.amount"];
    // amountSum的值是$5935.00
    

    Array Operators

    valueForKeyPath:返回一个right key path所指向属性的数组,这个属性是集合元素相关联的.

    如果所有返回的属性都是nil,valueForKeyPath:方法会抛出异常

    @distinctUnionOfObjects

    会返回right key path所指向对象的数组,是重复的将被忽略

    NSArray *distinctPayees = [self.transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"];
    //distinctPayees的结果:Car Loan, General Cable, Animal Hospital, Green Power, Mortgage.
    

    @unionOfObjects

    和@distinctUnionOfObjects相似的行为,但这个并不移除重复的对象.

    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.注意有重复的.
    

    Nesting Operators

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

    moreTransactions里的数据

    payee (values) amount (values formatted as currency) date (values formatted as month day, year)
    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

    NSArray *collectedDistinctPayees = [arrayOfArrays valueForKeyPath:@"@distinctUnionOfArrays.payee"];
    // collectedDistinctPayees的结果:Hobby Shop, Mortgage, Animal Hospital, Second Mortgage, Car Loan, General Cable - Cottage, General Cable, Green Power
    

    @unionOfArrays

    和@distinctUnionOfArrays一样,但包含重复的数据

    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.

    @distinctUnionOfSets

    这个和@distinctUnionOfArrays是一样的,只不过它用在NSSet实例包含NSSet实例,而不是NSArray包含NSArray.并且,它也是返回一个NSSet实例.

    相关文章

      网友评论

        本文标题:KVC---使用集合运算

        本文链接:https://www.haomeiwen.com/subject/ttswattx.html