NSSet的用法

作者: coming_168 | 来源:发表于2019-09-26 11:22 被阅读0次

集合

  • 集合特点:
    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

相关文章

  • NSSet的用法

    集合 集合特点:1⃣️存储的元素互不相同(若在集合中放入两个相同的元素,会自动过滤掉其中一个)2⃣️存储元素是无序...

  • NSSet 用法详解

    iOS 集合NSSet 用法详解 - CSDN博客 1、集合:集合(NSSet)和数组(NSArray)有相似之处...

  • NSSet、NSMutableSet基本用法

    NSSet、NSMutableSet基本用法 在Foundation框架中,提供了NSSet类,它是一组单值对象的...

  • iOS 集合NSSet 用法详解

    http://blog.csdn.net/jeffasd/article/details/50678542

  • NSSet/NSDictionary/CGPoint/NSNum

    NSSet NSSet 里的数据是不能重复的NSSet *set=[NSSet setWithObjects:@"...

  • iOS数组、字典和集合

    数组、字典和集合的基本用法 数组和集合的区别: NSSet和NSArray都是对象容器,用于存储对象,属于集合; ...

  • 数据去除多余重复数据

    利用NSSet的AllObjects方法 NSSet *set = [NSSet setWithArray:res...

  • iOS绘图

    core graphics绘图 NSSet是无序的 NSSet使用NSEnumerator遍历 NSSet any...

  • iOS NSSet彩蛋

    关于NSSet和NSArray的区别,这里不再赘述,网上有非常多讲解的很优秀的文章,基础用法可参照以下文章 NSS...

  • 基础零碎

    1.NSArray和NSSet的区别 >.NSArray内存中存储地址连续,而NSSet不连续 >.NSSet...

网友评论

    本文标题:NSSet的用法

    本文链接:https://www.haomeiwen.com/subject/zpcfyctx.html