KVC不为人知的干货小技巧
这里给大家推荐一下一些不为人知的超实用的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 = @[@"a", @"b", @"a", @"c", @"d"];
NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"%@", newArray);
可以进行嵌套使用
NSArray *array = @[
@{@"name":@"ken"},
@{@"name":@"kenrry"},
@{@"name":@"ken"},
@{@"name":@"kenrry1008"}];
//根据name字段,来进行重复删除。
NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSLog(@"%@", newArray);