IOS集合NSSet與NSMutableSet知識點


 

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"

 

 

最近有個妹子弄的一個關於擴大眼界跟內含的訂閱號,每天都會更新一些深度內容,在這里如果你感興趣也可以關注一下(嘿對美女跟知識感興趣),當然可以關注后輸入:github 會有我的微信號,如果有問題你也可以在那找到我;當然不感興趣無視此信息;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM