IOS集合NSSet與NSMutableSet知識點,nssetnsmutableset

來源:互聯網
上載者:User

IOS集合NSSet與NSMutableSet知識點,nssetnsmutableset

 

NSSet在實際應用中與NSArray區別不大,但是如果你希望尋找NSArray中的某一個元素,則需要遍曆整個數組,效率低下。而NSSet在尋找某一特定的元素的時候則是根據hash演算法直接找到此元素的位置,效率高。 NSSet是一個無序的,管理對個對象的集合類,最大特點是集合中不允許出現重複對象,和數學上的集合含義是一樣的。除了無序,不許重複,其他功能和NSArray是一樣的;需要注意的是:NSSet,NSArray裡面只能添加cocoa對象,如果需要加入基礎資料型別 (Elementary Data Type)(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"//可變指數集合NSMutableIndexSetNSMutableIndexSet *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"

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.