美文网首页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

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

  • iOS valueForKeyPath常用用法

    iOS valueForKeyPath常用用法 iOS valueForKeyPath常用用法

  • iOS开发中很实用的技巧

    (一)valueForKeyPath的使用 valueForKeyPath和valueForKey有一些类似,但...

  • 笔记

    数组取值 1.使用 valueForKeyPath valueForKeyPath 取到所有当前key的 valu...

  • 你不知道的valueForKeyPath

    如果说valueForKeyPath只是小小的tips,那真的可以说valueForKeyPath是偏方治大病。 ...

  • iOS之valueForKeyPath

    使用valueForKeyPath 大家好,我是亮亮。今天要说的是valueForKeyPath方法,而不是数组取...

  • valueForKeyPath

    可能大家对- (id)valueForKeyPath:(NSString *)keyPath方法不是很了解。 其实...

  • valueForKeyPath

    1. 数组求和 如果一个数组是由NSNumber或者数字的字符串组成的,可以直接进行进行求和: 2. 数组内字符串...

  • valueForKeyPath

    体验过OC中的: 自然也想用swift玩一把,简单的取值如下:

  • valueForKeyPath

    - (id)valueForKeyPath:(NSString *)keyPath这个方法非常的强大,举个例子: ...

网友评论

    本文标题:神奇的valueForKeyPath

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