国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS集合NSSet與NSMutableSet知識點

2019-11-14 18:40:26
字體:
來源:轉載
供稿:網友

 

NSSet在實際應用中與NSArray區別不大,但是如果你希望查找NSArray中的某一個元素,則需要遍歷整個數組,效率低下。而NSSet在查找某一特定的元素的時候則是根據hash算法直接找到此元素的位置,效率高。 NSSet是一個無序的,管理對個對象的集合類,最大特點是集合中不允許出現重復對象,和數學上的集合含義是一樣的。除了無序,不許重復,其他功能和NSArray是一樣的;需要注意的是:NSSet,NSArray里面只能添加cocoa對象,如果需要加入基本數據類型(int,float,BOOL,double等),需要將數據封裝成NSNumber類型。主要是一些常見的操作,別外一些操作見其相應的文檔,下面的代碼部分還運用的第三方插件BlocksKit相結合;

1:NSSet一些常見的操作

NSSet *newSet=[NSSet setWithObjects:@"wujy",@"VEVb",@"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"

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德清县| 庆阳市| 浦县| 连州市| 建始县| 孝义市| 陆丰市| 台中市| 岢岚县| 容城县| 应用必备| 文山县| 定州市| 宿迁市| 亚东县| 夏津县| 大埔区| 建宁县| 昌都县| 巴东县| 通河县| 永泰县| 南岸区| 陵川县| 澄迈县| 扎兰屯市| 深州市| 安福县| 永年县| 临澧县| 镇赉县| 襄垣县| 河源市| 固原市| 克东县| 苗栗县| 翁源县| 达孜县| 常熟市| 湟源县| 比如县|