iOS开发-一些KVC小知识

一、获取数组里的最大、最小、平均、求和

        NSArray *array = @[@"1",@"2",@"3",@"4",@"5"];
        NSNumber *sum = [array valueForKeyPath:@"@sum.floatValue"];
        NSNumber *avg = [array valueForKeyPath:@"@avg.floatValue"];
        NSNumber *max = [array valueForKeyPath:@"@max.floatValue"];
        NSNumber *min = [array valueForKeyPath:@"@min.floatValue"];
        NSLog(@"sum=%@",sum);
        NSLog(@"avg=%@",avg);
        NSLog(@"max=%@",max);
        NSLog(@"min=%@",min);

二、过滤掉重复的数据

        NSArray *array = @[@"zz",@"qwe",@"nn",@"nn",@"qwe",@"hh"];
        NSArray *filterArray = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
        NSLog(@"filterArray is %@",filterArray);

三、根据字典中的键的值过滤掉相同的数据

        NSArray *array = @[ @{@"title":@"zxd",@"name":@"zhangxuedong1"}, @{@"title":@"zxd1",@"name":@"zhangxuedong2"}, @{@"title":@"zxd2",@"name":@"zhangxuedong3"}, @{@"title":@"zxd",@"name":@"zhangxuedong1"}];
        
        NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.name"];
        
        NSLog(@"%@", newArray);

四、实例化方法调用

        NSArray *array = @[@"zz",@"qwe",@"nn",@"nn",@"qwe",@"hh"];
        NSArray *upperArray = [array valueForKeyPath:@"uppercaseString"];
        NSLog(@"upperArray%@",upperArray);

你可能感兴趣的:(iOS开发-一些KVC小知识)