NSSet 集合
最有用的功能:去重,排序
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 利用set不会重复添加元素的特性去重
NSArray *array = @[@22,@44,@123,@33,@33,@22,@44];
NSSet *set = [NSSet setWithArray: array];
NSLog(@"%@",[set allObjects]);
NSArray *array1 = @[@22,@44,@123,@33,@33,@22,@44];
//去重,利用字典Dictionary本身不能有key,value重复的特性去重。
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[array enumerateObjectsUsingBlock:^(id _Nonnull key, NSUInteger idx, BOOL *_Nonnull stop) {
dict[key] =@1;
}];
array1 = [dict allKeys];
NSLog(@"%@", array1);
//可以用index 索引
NSOrderedSet *orderSet = [NSOrderedSet orderedSet];
}
@end
冒泡排序
NSMutableArray *array3 = @[@22,@44,@123,@33,@33,@22,@44];
for (int i = 0; i < array3.count; i++) {
for (int j = 0; j < array3.count - 1 - i; j++) {
if (array3[j] > array3[j + 1]) {
[array3 exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
[array3 sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnullobj2) {
if (obj1 > obj2) {
//降序
return NSOrderedDescending;
}else if(obj1 < obj2){
//升序
return NSOrderedAscending;
}
else {
return NSOrderedSame;
}
}];
NSLog(@"--->%@", array3);