0
点赞
收藏
分享

微信扫一扫

iOS NSSet、NSMutableSet、NSIndexSet、NSMutableIndexSet

搬砖的小木匠 2021-09-19 阅读 66
发布文本
一、集合(NSSet)和数组(NSArray)区别

1、集合:集合(NSSet)和数组(NSArray)有相似之处,都是存储不同的对象的地址;不过NSArray是有序的集合,NSSet是无序的集合。
2、集合是一种哈希表,运用散列算法,查找集合中的元素比数组速度更快,但是它没有顺序。
3、NSSet和NSArray功能性质一样,用于存储对象,属于集合。但是和NAArray不一样的是它属于 “无序集合”,在内存中存储方式是不连续的,而NSArray是 “有序集合” 它内存中存储位置是连续的。

二、使用
  • NSMutableSet
    1、 创建NSMutableSet
 // 集合NSSet -> NSSet中不能存在重复的对象
        NSMutableSet *set01 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil];
        NSMutableSet *set02 = [[NSMutableSet alloc] initWithObjects:@"1",@"5",@"6", nil];

2、取并集

        [set01 unionSet:set02];   //取并集1,2,3,5,6
        NSLog(@"取并集:%@",set01);

3、取交集

        [set01 intersectSet:set02];  //取交集1
        NSLog(@"取交集:%@",set01);

4、删除set1中与set2相同的元素

        [set01 minusSet:set02];    //删除set1中与set2相同的元素 结果为:2, 3
        NSLog(@"删除set1中与set2相同的元素:%@",set01);
  • NSSet
    1、创建NSSet
        //创建
        NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
        [set count]; //返回集合中对象的个数
        NSLog(@"%ld",[set count]);

2、判断集合中是否拥有@“two”

        //判断集合中是否拥有@“two”
        BOOL ret = [set containsObject:@"two"];
        NSLog(@"%@",ret?@"有":@"无");

3、判断两个集合是否相等

        NSSet * set2 = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
        //判断两个集合是否相等
        BOOL ret1 = [set isEqualToSet:set2];
        NSLog(@"%@",ret1?@"相等":@"不等");

4、判断set是否是set3的子集合

        NSSet * set3 = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five", nil];
        //判断set是否是set3的子集合
        BOOL ret2 = [set isSubsetOfSet:set3];
        NSLog(@"%@",ret2?@"是":@"不是");

5、集合也可以用枚举器来遍历

        //集合也可以用枚举器来遍历
        NSEnumerator * enumerator = [set objectEnumerator];
        NSString *str;
        while (str = [enumerator nextObject]) {
            NSLog(@"%@",str);
        }

6、通过数组来初始化集合(数组转换为集合)

        //通过数组来初始化集合(数组转换为集合)
        NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
        NSSet * set4 = [[NSSet alloc] initWithArray:array];
        NSLog(@"%@",set4);

7、集合转换为数组

        //集合转换为数组
        NSArray * array2 = [set allObjects];
        NSLog(@"%@",array2);
  • NSMutableSet
    1、创建可变集合NSMutableSet
        //2.可变集合NSMutableSet
        //可变集合NSMutableSet
        NSMutableSet * set5 = [[NSMutableSet alloc] init];

2、添加元素

        [set5 addObject:@"one"];
        [set5 addObject:@"two"];
        [set5 addObject:@"two"]; //如果添加的元素有重复,实际只保留一个
        NSLog(@"%@",set5);

3、删除(指定元素)

        //删除(指定元素)
        [set5 removeObject:@"two"];
        NSLog(@"%@",set5);

4、删除(所有元素)

        //删除(所有元素)
        [set5 removeAllObjects];
        NSLog(@"%@",set5);

5、将set6中的元素添加到set5中来,如果有重复,只保留一个

        //将set6中的元素添加到set5中来,如果有重复,只保留一个
        NSSet * set6 = [[NSSet alloc] initWithObjects:@"two",@"three",@"four", nil];
        [set5 unionSet:set6];
        NSLog(@"%@",set5);

6、删除set5中与set6相同的元素

        //删除set5中与set6相同的元素
        [set5 minusSet:set6];
        NSLog(@"%@",set5);
  • NSIndexSet
    1、创建指数集合(索引集合)NSIndexSet
        //3、指数集合(索引集合)NSIndexSet
        //创建 -> 指数集合(索引集合)NSIndexSet
        NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)]; //集合中的数字是123

2、根据集合提取数组中指定位置的元素

        //根据集合提取数组中指定位置的元素
        NSArray * array01 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
        NSArray * newArray = [array01 objectsAtIndexes:indexSet]; //返回@"two",@"three",@"four"
        NSLog(@"%@",newArray);
  • NSMutableIndexSet
    1、创建可变指数集合NSMutableIndexSet
       // 4、可变指数集合NSMutableIndexS
        NSMutableIndexSet *indexSet02 = [[NSMutableIndexSet alloc] init];

2、添加

        [indexSet02 addIndex:0];
        [indexSet02 addIndex:3];
        [indexSet02 addIndex:5];

3、通过集合获取数组中指定的元素

        //通过集合获取数组中指定的元素
        NSArray *array02 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six", nil];
        NSArray *newArray02 = [array02 objectsAtIndexes:indexSet02]; //返回@"one",@"four",@"six"
        NSLog(@"%@",newArray02);

https://blog.csdn.net/siwen1990/article/details/52702799
https://blog.csdn.net/jeffasd/article/details/50678542
http://www.cnblogs.com/GISerYang/p/3340937.ht

举报

相关推荐

0 条评论