1、将数组中的单词做大小写转换:
NSArray *arr = @[@"Hello",@"Word",@"Objective",@"c"];
NSArray *arrUpper = [arr valueForKeyPath:@"uppercaseString"];
NSLog(@"%@",arrUpper);
NSArray *arrLower = [arr valueForKeyPath:@"lowercaseString"];
NSLog(@"%@",arrLower);
运行结果:
2019-04-20 15:49:06.968723+0800 Demo[4177:143487] (
HELLO,
WORD,
OBJECTIVE,
C
)
2019-04-20 15:49:06.969021+0800 Demo[4177:143487] (
hello,
word,
objective,
c
)
2、获取数组中所有单词的长度:
NSArray *arrLength = [arr valueForKeyPath:@"length"];
NSLog(@"%@",arrLength);
运行结果:
2019-04-20 15:49:06.969107+0800 Demo[4177:143487] (
5,
4,
9,
1
)
3、计算数组中所有对象某个属性的平均值:
NSArray *arrRate = @[@{@"orderId":@"0001",@"overtimeRate":@"0.6"},
@{@"orderId":@"0002",@"overtimeRate":@"0.2"},
@{@"orderId":@"0003",@"overtimeRate":@"0.0"},
@{@"orderId":@"0004",@"overtimeRate":@"0.5"}];
NSNumber *avg = [arrRate valueForKeyPath:@"@avg.overtimeRate.floatValue"];
NSLog(@"avg = %.2f %%",[avg floatValue] *100);
运行结果:
2019-04-20 15:49:09.644375+0800 Demo[4177:143487] avg = 32.50 %
备注:
和计算key:@"@sum.floatValue"
取最大值key:@"@max.floatValue"
取最小值key:@"@min.floatValue"
4、设置UITextField 的placeholder 的文本颜色:
[passwordTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
5、获取数组对象某个key的所有内容
NSArray *arrRate = @[@{@"orderId":@"0001",@"overtimeRate":@"0.6"},
@{@"orderId":@"0002",@"overtimeRate":@"0.2"},
@{@"orderId":@"0003",@"overtimeRate":@"0.0"},
@{@"orderId":@"0004",@"overtimeRate":@"0.5"}];
NSArray *arrRates = [arrRate valueForKeyPath:@"overtimeRate"];
NSLog(@"%@",arrRates);
运行结果:
2019-04-20 18:08:26.595803+0800 Demo[8980:223529] (
"0.6",
"0.2",
"0.0",
"0.5"
)