NSArray *array = @[@10, @23, @43, @54, @7, @17, @5];
+ (NSString *)caculateArray:(NSArray *)array
{
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%fn%fn%fn%f",sum,avg,max,min);
return [NSString stringWithFormat:@"%f",sum];
}
如下所示:
NSArray *arr = @[@{@"city":@"beijing",@"person":@{@"name":@"zhangsan"}},@{@"city":@"chengdu"}];
[arr valueForKeyPath:@"city"] 可以获取到city数组 @[@"beijing",@"chengdu"]
NSDictionary *dict1 = @{@"dic1":@{@"dic2":@{@"name":@"lisi",@"info":@{@"age":@"12"}}}};
id res = [dict1 valueForKeyPath:@"[dict1.dict2.name](http://dict1.dict2.name/)"];
NSArray *array = @[@"qq", @"wechat", @"qq", @"msn", @"wechat"];
[array valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSArray *array = @[@{@"name" : @"xiaoming",
@"code" : @1},
@{@"name": @"judy",
@"code" : @2},
@{@"name": @"judy",
@"code" : @3},
@{@"name": @"xiaohua",
@"code" : @4}];
[array valueForKeyPath:@"@distinctUnionOfObjects.name"];
[addressTextField setValue:[UIColor redColor] forKeyPath:@”_placeholderLabel.textColor”];
比起重写 - (void)drawPlaceholderInRect:(CGRect)rect; 要方便太多!
作者:逍遥晨旭
链接:https://www.jianshu.com/p/684fbea0a4c8