以前一直沒有用到過valueForKeyPath,一直以為和valueForKey差不多,可是今天一看才嚇了一跳,功能好強大啊.
objectForKey/valueForKey/valueForKeyPath區(qū)分
objectForKey是NSDictionary的一個方法,用來通過key取得字典的值.只有字典可以調(diào)用這個方法
valueForKey 和valueForKeyPath是兩個KVC方法,所有的對象都可以調(diào)用, valueForKey可以通過屬性名獲取到這個屬性的值,而valueForKeyPath則可以實現(xiàn)多級取值.
功能一
對于@[@{key:value},@{key:value},@{key:value}]的數(shù)組(數(shù)組元素是字典的),通過同一個key可以取到value的集合(數(shù)組)
NSDictionary *dic1 = @{@"city":@"北京",@"count":@"22"};
NSDictionary *dic2 = @{@"city":@"上海",@"count":@"18"};
NSDictionary *dic3 = @{@"city":@"深圳",@"count":@"17"};
NSArray *arr = @[dic1,dic2,dic3];
NSLog(@"city:%@",[arr valueForKeyPath:@"city"]);
NSLog(@"count:%@",[arr valueForKeyPath:@"count"]);
輸出結(jié)果:
2016-08-03 15:07:05.276 ValueForKeyPath使用[5181:192059] city:(
"北京",
"上海",
"深圳"
)
2016-08-03 15:07:05.276 ValueForKeyPath使用[5181:192059] count:(
22,
22,
18,
17
)
功能二
針對功能一,還有增強版,可以計算平均值/求和等操作
NSLog(@"求和:%@",[arr valueForKeyPath:@"@sum.count"]);
NSLog(@"平均:%@",[arr valueForKeyPath:@"@avg.count"]);
NSLog(@"最大:%@",[arr valueForKeyPath:@"@max.count"]);
NSLog(@"最小:%@",[arr valueForKeyPath:@"@min.count"]);
輸出結(jié)果
2016-08-03 15:07:05.277 ValueForKeyPath使用[5181:192059] 求和:79
2016-08-03 15:07:05.278 ValueForKeyPath使用[5181:192059] 平均:19.75
2016-08-03 15:07:05.278 ValueForKeyPath使用[5181:192059] 最大:22
2016-08-03 15:07:05.278 ValueForKeyPath使用[5181:192059] 最小:17
功能三
對于@{key1:@{key2:vale}}的字典(字典的value是另一個字典),通過key1.key2的鏈式的方式得到最深層的字典的值
NSDictionary *dict4 = @{@"name":@"小明",@"age":@"22"};
NSDictionary *dict5 = @{@"student":dict4};
NSDictionary *dict6 = @{@"class":dict5};
NSDictionary *dict7 = @{@"school":dict6};
NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.name"]);
NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.age"]);
輸出結(jié)果
2016-08-03 15:07:05.278 ValueForKeyPath使用[5181:192059] 小明
2016-08-03 15:07:05.279 ValueForKeyPath使用[5181:192059] 22
功能四
針對于功能三,不只是字典套字典,對象套對象/對象套對象再套字典等情況,都可以通過鏈式調(diào)用到深層的值
Student *student1 = [[Student alloc] init];
student1.name = @"小紅";
student1.info = @{@"phone":@"13102212345",@"mail":@"xiaohong@qq.com"};
School *school = [[School alloc] init];
school.student = student1;
NSLog(@"%@",[school valueForKeyPath:@"student.name"]);
NSLog(@"%@",[school valueForKeyPath:@"student.info.phone"]);
輸出結(jié)果
2016-08-03 15:21:38.258 ValueForKeyPath使用[5261:202278] 小紅
2016-08-03 15:21:38.258 ValueForKeyPath使用[5261:202278] 13102212345
2016-08-03 15:21:38.259 ValueForKeyPath使用[5261:202278] xiaohong@qq.com
Demo代碼
ViewController.m
#import "ViewController.h"
#import "Student.h"
#import "School.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *dic1 = @{@"city":@"北京",@"count":@"22"};
NSDictionary *dic2 = @{@"city":@"上海",@"count":@"18"};
NSDictionary *dic3 = @{@"city":@"深圳",@"count":@"17"};
NSArray *arr = @[dic1,dic2,dic3];
NSLog(@"city:%@",[arr valueForKeyPath:@"city"]);
NSLog(@"count:%@",[arr valueForKeyPath:@"count"]);
NSLog(@"求和:%@",[arr valueForKeyPath:@"@sum.count"]);
NSLog(@"平均:%@",[arr valueForKeyPath:@"@avg.count"]);
NSLog(@"最大:%@",[arr valueForKeyPath:@"@max.count"]);
NSLog(@"最小:%@",[arr valueForKeyPath:@"@min.count"]);
NSDictionary *dict4 = @{@"name":@"小明",@"age":@"22"};
NSDictionary *dict5 = @{@"student":dict4};
NSDictionary *dict6 = @{@"class":dict5};
NSDictionary *dict7 = @{@"school":dict6};
NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.name"]);
NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.age"]);
Student *student1 = [[Student alloc] init];
student1.name = @"小紅";
student1.info = @{@"phone":@"13102212345",@"mail":@"xiaohong@qq.com"};
School *school = [[School alloc] init];
school.student = student1;
NSLog(@"%@",[school valueForKeyPath:@"student.name"]);
NSLog(@"%@",[school valueForKeyPath:@"student.info.phone"]);
NSLog(@"%@",[school valueForKeyPath:@"student.info.mail"]);
}
@end
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,strong) NSDictionary *info;
@property(nonatomic,strong) NSString *name;
@end
School.h
#import <Foundation/Foundation.h>
#import "Student.h"
@interface School : NSObject
@property(nonatomic,strong) Student *student;
@end