iOS精品文章

KVC 中的 valueForKeyPath 在数组里的一些用法

2018-04-08  本文已影响6人  春暖花已开
NSArray *array = @[@"XIAOming", @"xiaohua", @"zhangshan", @"lisi", @"777"];
NSLog(@"%@", [array valueForKey:@"uppercaseString"]);
NSLog(@"%@", [array valueForKey:@"lowercaseString"]);
大小写转换结果
NSArray *array = @[@"XIAOming", @"xiaohua", @"zhangshan", @"lisi", @"777"];
NSLog(@"%@", [array valueForKey:@"length"]);
字符串长度
NSArray *array = @[@"1", @"1", @1, @"hehe"];
NSLog(@"\n%@\n%@\n%@\n%@",
          [array valueForKeyPath:@"@sum.floatValue"],
          [array valueForKeyPath:@"@avg.floatValue"],
          [array valueForKeyPath:@"@max.floatValue"],
          [array valueForKeyPath:@"@min.floatValue"]);
数组求和、平均数、最大值、最小值
NSArray *array = @[@"1", @"1", @1, @"hehe"];
NSLog(@"%@", [array valueForKeyPath:@"@distinctUnionOfObjects.self"]);
删除重复数据
 NSArray *array = @[@{@"name" : @"xiaoming",
                         @"code" : @1},
                       @{@"name": @"judy",
                         @"code" : @2},
                       @{@"name": @"judy",
                         @"code" : @3},
                       @{@"name": @"xiaohua",
                         @"code" : @4},
                       @{@"name": @"xiaoming",
                         @"code" : @4}];
NSLog(@"%@", [array valueForKeyPath:@"name"]);
快速找出字典数组key对应的值
 NSArray *array = @[@{@"name" : @"xiaoming",
                         @"code" : @1},
                       @{@"name": @"judy",
                         @"code" : @2},
                       @{@"name": @"judy",
                         @"code" : @3},
                       @{@"name": @"xiaohua",
                         @"code" : @4},
                       @{@"name": @"xiaoming",
                         @"code" : @4}];
NSLog(@"%@", [array valueForKeyPath:@"@distinctUnionOfObjects.name"]);
删除重复的数据,嵌套版
NSArray *temp1 = @[@3, @2, @2, @1];
NSArray *temp2 = @[@3, @4, @5];
    
NSLog(@"\n%@",[@[temp1, temp2] valueForKeyPath:@"@distinctUnionOfArrays.self"]);
NSLog(@"\n%@",[@[temp1, temp2] valueForKeyPath:@"@unionOfArrays.self"]);
将多个数组去重合并和直接合并
上一篇下一篇

猜你喜欢

热点阅读