第一种方法NSArray
NSArray *array = [[NSArray alloc]initWithObjects:@"2",@"1",@"2",@"3",nil];
//去重有序的 新建一个数组判断是否包含
NSMutableArray *mArray = [[NSMutableArray alloc]init];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![mArray containsObject:obj]) {
[mArray addObject:obj];
}
}];
NSLog(@"%@",mArray);
第二种方法NSOrderedSet
NSArray *array = [[NSArray alloc]initWithObjects:@"2",@"1",@"2",@"3",nil];
NSOrderedSet *orderSet = [NSOrderedSet orderedSetWithArray:array];
NSArray *newArray = orderSet.array;
NSLog(@"%@",newArray);
第三种方法 NSMutableDictionary
NSArray *array = [[NSArray alloc]initWithObjects:@"2",@"1",@"2",@"3",nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[dict setValue:obj forKey:obj];
}];
NSArray *tempArray = dict.allValues;
NSLog(@"%@",tempArray);
网友评论