美文网首页
KVC中的函数对集合进行操作

KVC中的函数对集合进行操作

作者: 邓布利多教授 | 来源:发表于2019-07-10 13:51 被阅读0次

    集合操作符

    NSArray *array = @[@10, @20, @30, @40];
        
        //求和
        NSNumber *sum = [array valueForKeyPath:@"@sum.self"];
        NSLog(@"sum = %ld",sum.integerValue);
        
        //平均值
        NSNumber *avg = [array valueForKeyPath:@"@avg.self"];
        NSLog(@"avg = %ld",avg.integerValue);
        
        //元素个数
        NSNumber *count = [array valueForKeyPath:@"@count"];
        NSLog(@"count = %ld",count.integerValue);
        
        //最大值
        NSNumber *max = [array valueForKeyPath:@"@max.self"];
        NSLog(@"max = %ld",max.integerValue);
        
        //最小值
        NSNumber *min = [array valueForKeyPath:@"@min.self"];
        NSLog(@"min = %ld",min.integerValue);
    

    对象操作符

    NSArray *array = @[@"123", @"123", @"456", @"789", @"012"];
        
        //返回一个数组
        NSArray *arr1 = [array valueForKeyPath:@"@unionOfObjects.self"];
        NSLog(@"arr1 = %@",arr1);
        
        //返回一个去重的数组
        NSArray *arr2 = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
        NSLog(@"arr2 = %@",arr2);
    

    相关文章

      网友评论

          本文标题:KVC中的函数对集合进行操作

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