·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> IOS集合NSSet与NSMutableSet知识点

IOS集合NSSet与NSMutableSet知识点

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

 

NSSet在实际应用中与NSArray区别不大,但是如果你希望查找NSArray中的某一个元素,则需要遍历整个数组,效率低下。而NSSet在查找某一特定的元素的时候则是根据hash算法直接找到此元素的位置,效率高。 NSSet是一个无序的,管理对个对象的集合类,最大特点是集合中不允许出现重复对象,和数学上的集合含义是一样的。除了无序,不许重复,其他功能和NSArray是一样的;需要注意的是:NSSet,NSArray里面只能添加cocoa对象,如果需要加入基本数据类型(int,float,BOOL,double等),需要将数据封装成NSNumber类型。主要是一些常见的操作,别外一些操作见其相应的文档,下面的代码部分还运用的第三方插件BlocksKit相结合;

1:NSSet一些常见的操作

NSSet *newSet=[NSSet setWithObjects:@"wujy",@"cnblogs",@"age",nil];
    NSLog(@"set的个数为:%ld",[newSet count]);
    
    //不会按上面增加的顺序输入 所以NSSET是没有顺序
    NSEnumerator *enumeratorset=[newSet objectEnumerator];
    for (NSObject *obj in enumeratorset) {
        NSLog(@"key为:%@",obj);
    }
    
    //是否存在
    BOOL isExict=[newSet containsObject:@"wujy"];
    NSLog(@"是否存在:%d",isExict);
    
    //返回任意一个元素
    NSString *anyObje=[newSet anyObject];
    NSLog(@"返回任意一个元素:%@",anyObje);
    
    //判断是否包含这个元素 并还回
    NSObject *memberObj=[newSet member:@"age"];
    if (memberObj) {
        NSLog(@"存在元素(age):%@",memberObj);
    }
    
    //简便创建nsset
    NSSet *twoSet=[[NSSet alloc] initWithArray:@[@2,@10,@12,@"wujy"]];
    
    //判断两个nsset是否相等
    BOOL isEaual=[newSet isEqualToSet:twoSet];
    NSLog(@"判断两个nsset是否相等(0):%d",isEaual);
    
    //判断两个nsset是否交集
    BOOL isInterSects=[newSet intersectsSet:twoSet];
    NSLog(@"判断两个nsset是否交集(1):%d",isInterSects);
    
    //Blocks
    
    NSSet *blockSet=[[NSSet alloc]initWithObjects:@1,@2,@3,@4,@5,@6,nil];
    //遍历
    [blockSet bk_each:^(id obj) {
        NSLog(@"block遍历的值为:%@",obj);
    }];
    
    //过滤
    NSSet *selectSet=[blockSet bk_select:^BOOL(id obj) {
        BOOL select=[obj intValue]>3?YES:NO;
        return select;
    }];
    NSLog(@"过滤后的nsset(6,5,4):%@",selectSet);
    
    //过滤 只在第一个符合条件时就停止
    NSSet *matchSet=[blockSet bk_match:^BOOL(id obj) {
        BOOL select=[obj intValue]<4?YES:NO;
        return select;
    }];
    NSLog(@"matchSet过滤后的nsset(3):%@",matchSet);
    
    //取反过滤
    NSSet *rejectSet=[blockSet bk_reject:^BOOL(id obj) {
        BOOL select=[obj intValue]<4?YES:NO;
        return select;
    }];
    NSLog(@"取反过滤后的nsset(6,5,4):%@",rejectSet);
    
    //对各个项进行处理
    NSSet *mapSet=[blockSet bk_map:^id(id obj) {
        return @([obj intValue]+1);
    }];
    NSLog(@"修改后的值为(7,3,6,2,5,4):%@",mapSet);
    
    //是否符合条件 返回bool
    BOOL isSelected=[blockSet bk_any:^BOOL(id obj) {
        BOOL select=[obj intValue]>3?YES:NO;
        return select;
    }];
    NSLog(@"%d符合条件1",isSelected);
    
    //判断是否所有的项都符合这个条件
    BOOL allSelected=[blockSet bk_all:^BOOL(id obj) {
        BOOL select=[obj intValue]>2?YES:NO;
        return select;
    }];
    NSLog(@"%d符合条件0",allSelected);
    
    //判断是否所有的项都不符合这个条件
    BOOL noneSelected=[blockSet bk_none:^BOOL(id obj) {
        BOOL select=[obj intValue]>50?YES:NO;
        return select;
    }];
    NSLog(@"%d符合条件1",noneSelected);

 

2:NSMutableSet一些常见的操作

 

    //创建
    NSMutableSet *mutableSet=[[NSMutableSet alloc]initWithCapacity:5];
    [mutableSet addObject:@1];
    [mutableSet addObjectsFromArray:@[@2,@3,@4]];
    NSLog(@"增加后的NSMutableSet(3,2,1,4):%@",mutableSet);
    
    //把nsset增加进去
    [mutableSet unionSet:[NSSet setWithObjects:@5,@6,nil]];
    NSLog(@"增加后的NSMutableSet(3,6,2,5,1,4):%@",mutableSet);
    
    //去除元素
    [mutableSet removeObject:@5];
    NSLog(@"去除后的元素(3,6,2,1,4):%@",mutableSet);
    
    //删除nsset里面的
    [mutableSet minusSet:[NSSet setWithObjects:@1,@2,nil]];
    NSLog(@"去除后nsset的元素(3,6,4):%@",mutableSet);
    
    //nsset转化成nsmutableset
    NSSet *newSet=[NSSet setWithObjects:@10,@11,@12, nil];
    NSMutableSet *newMutableSet=[NSMutableSet set];
    [newMutableSet setSet:newSet];
    NSLog(@"转换后的mutableset值为(12,10,11):%@",newMutableSet);
    
    //Blocks
    //过滤
    NSMutableSet *blockMutableSet=[[NSMutableSet alloc]initWithObjects:@20,@23,@25,nil];
    [blockMutableSet bk_performSelect:^BOOL(id obj) {
        BOOL select=[obj intValue]>22?YES:NO;
        return select;
    }];
    NSLog(@"过滤后的nsmutableset(25,23):%@",blockMutableSet);
    
    //取反过滤
    [blockMutableSet bk_performReject:^BOOL(id obj) {
        BOOL select=[obj intValue]>24?YES:NO;
        return select;
    }];
    NSLog(@"取反过滤后的nsmutableset(23):%@",blockMutableSet);
    
    //对项进行修改
    [blockMutableSet bk_performMap:^id(id obj) {
        return @([obj intValue]+1);
    }];
    NSLog(@"修改后的nsmutableset(24):%@",blockMutableSet);

 

3:指数集合(索引集合)NSIndexSet

//指数集合(索引集合)NSIndexSet
 NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)]; //集合中的数字是123

//根据集合提取数组中指定位置的元素
NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSArray * newArray = [array objectsAtIndexes:indexSet]; //返回@"two",@"three",@"four"

//可变指数集合NSMutableIndexSet
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
[indexSet addIndex:0]
[indexSet addIndex:3];
[indexSet addIndex:5];
//通过集合获取数组中指定的元素
NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six", nil];
NSArray *newArray = [array objectsAtIndexes:indexSet]; //返回@"one",@"four",@"six"