溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

NSSet常用用法

發(fā)布時(shí)間:2020-07-16 08:09:26 來(lái)源:網(wǎng)絡(luò) 閱讀:1007 作者:Im劉亞芳 欄目:開(kāi)發(fā)技術(shù)
//集合初始化
        NSArray *array = [NSArray arrayWithObjects:@"aa", @"bb", @"cc", nil];
        NSSet *se = [NSSet setWithArray:array];
        NSLog(@"%@", se);
        //集合里面只有一個(gè)元素
        NSSet *set = [NSSet setWithObject:@"aa"];  //只能放一個(gè)
        NSLog(@"%@", set);
        NSSet *set1 = [NSSet setWithObjects:@"11",@"66",@"33",@"44",@"55",@"aa", nil];  //如果集合李有重復(fù)的會(huì)自動(dòng)合并
        NSLog(@"%@", set1);
        //集合里面是否包含另一個(gè)集合Returns a Boolean value that indicates whether every object in the receiving set is also present in another given set.
        //返回一個(gè)布爾值,表示接收組中的每一個(gè)對(duì)象是否還存在于另一個(gè)給定。
        BOOL b = [set isSubsetOfSet:set1];
        NSLog(@"http://///******//////%d",b);
        //返回一個(gè)任意元素,The object returned is chosen at the set’s convenience—the selection is not guaranteed to be random.
        //返回的對(duì)象選擇的設(shè)置convenience-the選擇不能保證是隨機(jī)的。
        NSString *set2 = [set1 anyObject];
        NSLog(@"%@", set2);
        //返回所有的元素,存放在一個(gè)書(shū)組中,
        NSArray *all = [set1 allObjects];  //調(diào)用所有的object
        NSLog(@"0.0%@", all);
        //計(jì)算集合長(zhǎng)度,個(gè)數(shù)
        NSLog(@"%ld", [set1 count]);        //計(jì)算集合長(zhǎng)度
        //判斷集合中是否包含某個(gè)對(duì)象
        BOOL a = [set1 containsObject:@"11"];  //判斷集合中是否包含某個(gè)對(duì)象
        NSLog(@"%d", a);
        
        //Returns an initialized mutable set with a given initial capacity.
        NSMutableSet *muset = [NSMutableSet setWithCapacity:5];
        //向里面存入元素
        NSArray *arr = [NSArray arrayWithObjects:@"22", @"33",@"11", nil];
        [muset addObjectsFromArray:arr];
        NSLog(@"muset == %@", muset);
        //移除元素
        [muset removeObject:@"22"];
        NSLog(@"%@", muset);
        
        //可變集合
        NSCountedSet *count = [NSCountedSet setWithObjects:@"22", @"33", @"33", @"66", nil];
        NSInteger coun = [count countForObject:@"33"]; //計(jì)算集合中重復(fù)元素的個(gè)數(shù)
        NSLog(@"%ld", coun);
        
        
        
        //集合類(lèi)型快速枚舉
        //書(shū)組中forin的快速遍歷
        NSArray *aaa = [NSArray arrayWithObjects:@"11", @"33", @"55", @"99", nil];
        for (int i = 0; i < [aaa count]; i++) {
            NSLog(@"aaa == %@", [aaa objectAtIndex:i]);
        }
        for (NSString *i in aaa) {
            NSLog(@"forin == %@", i);
        }
        //字典中forin的快速遍歷
        NSArray  *aaaa = [NSArray arrayWithObjects:@"11", @"22", @"33", @"44", nil];
        NSArray *bbbb = [NSArray arrayWithObjects:@"aa", @"bb", @"cc", @"dd", nil];
        NSMutableDictionary *cla = [NSMutableDictionary dictionaryWithObjects:bbbb forKeys:aaaa];
        
        NSLog(@"%@", cla);
        for (NSString *key in [cla allKeys]) {   //forin前面的類(lèi)型就是你要查詢(xún)的 類(lèi)型 ,,,切記 。
            NSLog(@"value == %@", [cla objectForKey:key]);
        }

集合

  • 數(shù)學(xué)中有集合讀概念

  • 比如:整數(shù)集,自然數(shù)集

  • 在比如:集合{1,2,5,}

集合特點(diǎn)

  • 存儲(chǔ)的元素互不相同

  • 存儲(chǔ)元素是無(wú)序的

  • 存儲(chǔ)元素必須是對(duì)象類(lèi)型

容器有三個(gè):NSArrey     NSdictionary   NSSet


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI