问题由组去重开始,随即看到keyValue,觉得有用,以后可以再深入学习。
数组去重的几种方法
- 字典的AllKeys
NSArray *dataArray = @[@"wf,@"wf2",@"wf3",@"wf"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for(NSString *str in dataArray){
[dic setValue:str forKey:str];
}
NSLog(@"%@",[dic allKeys]);
- 集合的AllObjects
NSSet *set = [NSSet setWithArray:dataArray];
NSLog(@"%@",[set allObjects]);
- 数组的containsObject 方法
NSMutableArray *listArr = [NSMutableArray array];
for(NSString *str in dataArray){
if(![listArr containsObject:str]){
[listArr addobject:str];
}
}
- keyValue的方法
dataArray = [dataArray valueForKeyPath:@"distinctUnionOfObjects.self"];
valueForKeyPath
使用示例
//数组内字符串写
NSArray *array = @[@"name", @"w", @"aa", @"jimsa"];
NSLog(@"%@", [array valueForKeyPath:@"uppercaseString"]);
//数组内字符长度
[array valueForKeyPath:@"length"];
或
NSArray *array = @[@1, @2, @3, @4, @10];
NSNumber *sum = [array valueForKeyPath:@"@sum.self"];
NSNumber *avg = [array valueForKeyPath:@"@avg.self"];
NSNumber *max = [array valueForKeyPath:@"@max.self"];
NSNumber *min = [array valueForKeyPath:@"@min.self"];
或
//得到字典中每一个key 对应的value
NSArray *array = @[@{@"name" : @"cook",@"code" : @1},
@{@"name": @"jim",@"code" : @2},
@{@"name": @"jim",@"code" : @1},
@{@"name": @"jbos",@"code" : @1}];
NSLog(@"%@", [array valueForKeyPath:@"name"]);
//也可以直接去重
[array valueForKeyPath:@"@distinctUnionOfObjects.name"];