NSSet的用法

集合

  • 集合特点:
    1⃣️存储的元素互不相同(若在集合中放入两个相同的元素,会自动过滤掉其中一个)
    2⃣️存储元素是无序的
    3⃣️存储元素必须是对象类型
    4⃣️集合是一种哈希表,运用散列算法,查找集合中的元素比数组速度更快

在iOS中的容器常用有三个:NSArrey \ NSdictionary \ NSSet,这里主要介绍一下NSSet的用法

1. NSSet创建方式
    NSSet *set = [NSSet setWithObjects:@"12",@"牛皮",@"12", nil];
    NSLog(@"set->%@,count->%lu",set,(unsigned long)set.count); //set->12,牛皮  count->2
    
    NSSet *set1 = [[NSSet alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSLog(@"set1->%@",set1); //set1->3,1,4,2,5
2. NSSet比较
    NSSet<NSString *> *set1 = [NSSet setWithObjects:@"12",@"Yes",@"56",@"caodan", nil];
    NSSet<NSString *> *set2 = [NSSet setWithObjects:@"bi", @"12",@"Yes", nil];
       
    // 是否子集合
    BOOL isBool = [set1 isSubsetOfSet:set2];
    NSLog(@"isBool->%d",isBool);    //isBool->0
    // 是否有交集
    isBool = [set1 intersectsSet:set2];
    NSLog(@"isBool->%d",isBool);    //isBool->1
    // 是否相等
    isBool = [set1 isEqualToSet:set2];
    NSLog(@"isBool->%d",isBool);    //isBool->0
3. 集合转数组
    NSSet *set = [NSSet set];
    set = [NSSet setWithArray:@[@"1",@"2",@"3",@"4",@"56789"]];
    NSArray *array = [set allObjects];
    NSLog(@"array->%@",array);//3,1,56789,4,2
4. 获取NSSet元素
    NSSet<NSString *> *set1 = [NSSet setWithObjects:@"12",@"Yes",@"56",@"caodan", nil];
    // 所有成员
    NSArray *array = set1.allObjects;
    NSLog(@"set1->%@", array);  //Yes,caodan,12,56
    // 随机获取
    NSString *anyObject = [set1 anyObject];
    NSLog(@"anyObject->%@", anyObject); //Yes
5. 过滤NSSet元素
    NSSet *set = [NSSet set];
    set = [NSSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    NSLog(@"set->%@,count->%ld",set,set.count);//set->3,1,4,2,5 ,count->5

    // 1.通过NSPredicate过滤
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([evaluatedObject integerValue] >= 3) {
            return NO;
        }
        return YES;
    }];
    set = [set filteredSetUsingPredicate:predicate];
    NSLog(@"set->%@,count->%ld",set,set.count);//set->1,2,count->2

    // 2.用block过滤
    set = [set objectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        if (obj.integerValue > 3) {
            return NO;
        }
        return YES;
    }];
    NSLog(@"set->%@",set);//set->2,1,3

6. 排序
    // 降序排列
    NSSortDescriptor *sortD = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];
    NSArray<NSSortDescriptor *> *sortDs = [NSArray arrayWithObjects:sortD, nil];
    NSArray<NSString *> *array = [set sortedArrayUsingDescriptors:sortDs];
    NSLog(@"%@", array); //56789,4,3,2,1
7. 可变集合创建
    // 快速创建
    NSMutableSet *set1 = [NSMutableSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    // init方法创建
    NSMutableSet *mSet = [[NSMutableSet alloc] init];
    [mSet addObject:@"9"];
8. 删除集合的元素
    // 在set1中删除set2中相同的元素
    NSMutableSet *set1 = [NSMutableSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    NSSet *set2 = [NSSet setWithArray:@[@"1",@"2"]];
    [set1 minusSet:set2];
    NSLog(@"set1->%@",set1);//set1->3,4,5

    // 删除指定元素
    NSMutableSet *set1 = [NSMutableSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    [set1 removeObject:@"3"];
    NSLog(@"set1->%@",set1);//set1->1,4,2,5
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 站在前辈的肩膀上前行 UIKit框架和Foundation框架 所有的Mac OS X和IOS程序都是由大量的对象...
    zysmoon阅读 8,778评论 0 16
  • 卷首语 欢迎来到 objc.io 第七期! 这个月,我们选择了 Foundation 框架作为我们的主题。 Fou...
    评评分分阅读 1,579评论 0 8
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,441评论 0 4
  • 一、数组类 数组是一个有序的集合,OC中的数组只能储存对象类型,但是对于对象的类型没有限制。 通过下标访问数组元素...
    刘可爱最可爱阅读 388评论 0 0