美文网首页iOS 艾欧艾斯
神奇的valueForKeyPath

神奇的valueForKeyPath

作者: ImmortalSummer | 来源:发表于2016-08-03 15:36 被阅读185次

    以前一直没有用到过valueForKeyPath,一直以为和valueForKey差不多,可是今天一看才吓了一跳,功能好强大啊.


    objectForKey/valueForKey/valueForKeyPath区分

    ** objectForKey是NSDictionary的一个方法,用来通过key取得字典的值.只有字典可以调用这个方法
    ** valueForKey
    和** valueForKeyPath**是两个KVC方法,所有的对象都可以调用, valueForKey可以通过属性名获取到这个属性的值,而valueForKeyPath则可以实现多级取值.

    功能一

    对于@[@{key:value},@{key:value},@{key:value}]的数组(数组元素是字典的),通过同一个key可以取到value的集合(数组)

        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"]);
    

    输出结果:

    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"]);
    

    输出结果

    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"]);
    

    输出结果

    2016-08-03 15:07:05.278 ValueForKeyPath使用[5181:192059] 小明
    2016-08-03 15:07:05.279 ValueForKeyPath使用[5181:192059] 22
    

    功能四

    针对于功能三,不只是字典套字典,对象套对象/对象套对象再套字典等情况,都可以通过链式调用到深层的值

        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"]);
    

    输出结果

    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
    

    相关文章

      网友评论

        本文标题:神奇的valueForKeyPath

        本文链接:https://www.haomeiwen.com/subject/neiqsttx.html