作者:孟令文
剛剛學(xué)習(xí)了Funcdation框架中的NSSet趟咆,跟大家分享一下呵扛。
1电湘、集合:集合(NSSet)和數(shù)組(NSArray)有相似之處隔节,都是存儲(chǔ)不同的對(duì)象的地址;不過NSArray是有序的集合寂呛,NSSet是無(wú)序的集合怎诫。
集合是一種哈希表,運(yùn)用散列算法贷痪,查找集合中的元素比數(shù)組速度更快幻妓,但是它沒有順序。
2劫拢、存儲(chǔ)的所有對(duì)象只能有唯一一個(gè)肉津,不能重復(fù)。
/****************? ? Immutable Set? ? ****************/
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", @"a", @"b", @"c", @"d",nil];
//直接類名調(diào)用舱沧,不用alloc妹沙。+號(hào)方法加號(hào)方法使用一組對(duì)象創(chuàng)建新的集合
NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];
//—號(hào)方法
NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil];
//把數(shù)組轉(zhuǎn)化成集合
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c",@"d", nil];
NSSet *set3? ? = [NSSet setWithArray:array];
//打印集合中的所有元素
NSLog(@"set:%@",set);
NSLog(@"set1 :%@", set1);
NSLog(@"set2 :%@", set2);
NSLog(@"set3 :%@", set3);
//獲取集合個(gè)數(shù)
NSLog(@"set count :%lu", set1.count);
NSLog(@"set count :%lu", [set1 count]);
//以數(shù)組的形式獲取集合中的所有對(duì)象
NSArray *allObj = [set3 allObjects];
NSLog(@"allObj :%@", allObj);
//是否包含某個(gè)對(duì)象
NSLog(@"Is set3 coatains a ?? %d", [set3 containsObject:@"a"]);
//是否包含指定set中的對(duì)象
NSLog(@"Is set1 contains set3's obj ?? %d", [set1 intersectsSet:set3]);
//是否完全匹配
NSLog(@"set1 isEqualto set3 :%d", [set1 isEqualToSet:set3]);
NSLog(@"set2 isEqualto set3 :%d", [set2 isEqualToSet:set3]);
//是否是子集合
NSLog(@"set3 isSubSet of set1:%d", [set3 isSubsetOfSet:set1]);
NSLog(@"set3 isSubSet of set :%d", [set3 isSubsetOfSet:set ]);
//set 加一個(gè) arrar 類型的對(duì)象
NSSet *set4 = [NSSet setWithObjects:@"a", @"b", nil];
NSArray *ary = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
NSSet *set5 = [set4 setByAddingObjectsFromArray:ary];
NSLog(@"addFromArray :%@", set5);
//set 加一個(gè) id 類型的對(duì)象
NSSet *set6 = [set5 setByAddingObject:@"c"];
NSLog(@"set6 is :%@",set6);
//set 加一個(gè) set 類型的對(duì)象
NSSet *set7 = [set1 setByAddingObjectsFromSet:set2];
NSLog(@"set7 is :%@",set7);
/****************? ? Mutable Set? ? ****************/
//初始化
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
[mutableSet addObject:@"1"];
NSLog(@"mutableSet1 is :%@",mutableSet);
//加號(hào)方法使用一組對(duì)象創(chuàng)建新的集合
NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];
NSLog(@"mutableSet1 is %@",mutableSet1);
//初始化一個(gè)新分配的集合,大小為size
NSMutableSet *mutableSet2 = [[NSMutableSet alloc] initWithCapacity:3];
[mutableSet2 addObject:@"1"];
[mutableSet2 addObject:@"b"];
[mutableSet2 addObject:@"3"];
//創(chuàng)建一個(gè)有size大小的新集合
NSMutableSet *mutableSet3 = [NSMutableSet setWithCapacity:3];
[mutableSet3 addObject:@"a"];
[mutableSet3 addObject:@"2"];
[mutableSet3 addObject:@"c"];
//集合元素相減
[mutableSet1 minusSet:mutableSet2];
NSLog(@"minus :%@", mutableSet1);
//只留下相等元素熟吏, 做交集運(yùn)算
[mutableSet2 intersectSet:mutableSet3];
NSLog(@"intersect :%@", mutableSet2);
//合并集合 將兩個(gè)集合中所有元素添加到調(diào)用者
[mutableSet2 unionSet:mutableSet3];
NSLog(@"union :%@", mutableSet2);
//刪除指定元素
[mutableSet2 removeObject:@"a"];
NSLog(@"removeObj :%@", mutableSet2);
//刪除所有數(shù)據(jù)
[mutableSet2 removeAllObjects];
NSLog(@"removeAll :%@", mutableSet2);
//清空接收,把自己清空然后接受另一個(gè)set傳過來(lái)的所有對(duì)象
NSMutableSet *mutableSet4 = [NSMutableSet setWithObjects:@"a",@"b",@"c" ,nil];
[mutableSet4 setSet:set2];
NSLog(@"removeAll :%@", mutableSet4);
/****************? ? Counted Set? ? ****************/
//NSCountedSet類聲明一個(gè)可變的編程接口,無(wú)序模糊對(duì)象的集合距糖。一組數(shù)也稱為一個(gè)袋子。概述每個(gè)不同的對(duì)象插入一個(gè)NSCountedSet對(duì)象有一個(gè)與之關(guān)聯(lián)的計(jì)數(shù)器牵寺。
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithObjects:@"1",@"2",@"3",@"2",@"1", nil];
NSLog(@"countedSet is :%@",countedSet);