1、求值运算
NSArray * arr = @[@"1.0",@"2.0",@"3.0",@"4.2",@"5.1"];
// NSNumber * num = [arr valueForKeyPath:@"@sum.floatValue"];//求和
// NSNumber * num = [arr valueForKeyPath:@"@avg.floatValue"];//求平均值
// NSNumber * num = [arr valueForKeyPath:@"@max.floatValue"];//求最大值
NSNumber * num = [arr valueForKeyPath:@"@min.floatValue"];//求最小值
NSLog(@"%@",num);
2、获取数组中相同key的元素
NSArray * arr = @[@{@"name":@"xiaoming",@"age":@123},@{@"name":@"xiaogou",@"age":@12},@{@"name":@"xiaohong",@"age":@13},@{@"name":@"xiaolong",@"age":@23}];
NSLog(@"%@",[arr valueForKeyPath:@"name"]); //返回数组 @[@"xiaoming",@"xiaogou",@"xiaohong",@"xiaolong"];
3、当多个字典相互嵌套时,取子层级中的属性就非常简单了
NSDictionary * dic = @{@"one":@{@"two":@"twoResult"},@"oneOther":@{@"twoOther":@"twoOtherResult"}};
NSLog(@"%@",[dic valueForKeyPath:@"one.two"]);// twoResult
NSLog(@"%@",[dic valueForKeyPath:@"oneOther.twoOther"]);// twoOtherResult
目前常用这三种
NSArray * arr = @[@"1.0",@"2.0",@"3.0",@"4.2",@"5.1"];
// NSNumber * num = [arr valueForKeyPath:@"@sum.floatValue"];//求和
// NSNumber * num = [arr valueForKeyPath:@"@avg.floatValue"];//求平均值
// NSNumber * num = [arr valueForKeyPath:@"@max.floatValue"];//求最大值
NSNumber * num = [arr valueForKeyPath:@"@min.floatValue"];//求最小值
NSLog(@"%@",num);
2、获取数组中相同key的元素
NSArray * arr = @[@{@"name":@"xiaoming",@"age":@123},@{@"name":@"xiaogou",@"age":@12},@{@"name":@"xiaohong",@"age":@13},@{@"name":@"xiaolong",@"age":@23}];
NSLog(@"%@",[arr valueForKeyPath:@"name"]); //返回数组 @[@"xiaoming",@"xiaogou",@"xiaohong",@"xiaolong"];
3、当多个字典相互嵌套时,取子层级中的属性就非常简单了
NSDictionary * dic = @{@"one":@{@"two":@"twoResult"},@"oneOther":@{@"twoOther":@"twoOtherResult"}};
NSLog(@"%@",[dic valueForKeyPath:@"one.two"]);// twoResult
NSLog(@"%@",[dic valueForKeyPath:@"oneOther.twoOther"]);// twoOtherResult
目前常用这三种