作者:孟令文
剛剛學習了Funcdation框架中的NSSet,跟大家分享一下。
1、集合:集合(NSSet)和數組(NSArray)有相似之處,都是存儲不同的對象的地址;不過NSArray是有序的集合,NSSet是無序的集合。
集合是一種哈希表,運用散列算法,查找集合中的元素比數組速度更快,但是它沒有順序。
2、存儲的所有對象只能有唯一一個,不能重復。
/****************? ? Immutable Set? ? ****************/
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", @"a", @"b", @"c", @"d",nil];
//直接類名調用,不用alloc。+號方法加號方法使用一組對象創建新的集合
NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];
//—號方法
NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil];
//把數組轉化成集合
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);
//獲取集合個數
NSLog(@"set count :%lu", set1.count);
NSLog(@"set count :%lu", [set1 count]);
//以數組的形式獲取集合中的所有對象
NSArray *allObj = [set3 allObjects];
NSLog(@"allObj :%@", allObj);
//是否包含某個對象
NSLog(@"Is set3 coatains a ?? %d", [set3 containsObject:@"a"]);
//是否包含指定set中的對象
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 加一個 arrar 類型的對象
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 加一個 id 類型的對象
NSSet *set6 = [set5 setByAddingObject:@"c"];
NSLog(@"set6 is :%@",set6);
//set 加一個 set 類型的對象
NSSet *set7 = [set1 setByAddingObjectsFromSet:set2];
NSLog(@"set7 is :%@",set7);
/****************? ? Mutable Set? ? ****************/
//初始化
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
[mutableSet addObject:@"1"];
NSLog(@"mutableSet1 is :%@",mutableSet);
//加號方法使用一組對象創建新的集合
NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];
NSLog(@"mutableSet1 is %@",mutableSet1);
//初始化一個新分配的集合,大小為size
NSMutableSet *mutableSet2 = [[NSMutableSet alloc] initWithCapacity:3];
[mutableSet2 addObject:@"1"];
[mutableSet2 addObject:@"b"];
[mutableSet2 addObject:@"3"];
//創建一個有size大小的新集合
NSMutableSet *mutableSet3 = [NSMutableSet setWithCapacity:3];
[mutableSet3 addObject:@"a"];
[mutableSet3 addObject:@"2"];
[mutableSet3 addObject:@"c"];
//集合元素相減
[mutableSet1 minusSet:mutableSet2];
NSLog(@"minus :%@", mutableSet1);
//只留下相等元素, 做交集運算
[mutableSet2 intersectSet:mutableSet3];
NSLog(@"intersect :%@", mutableSet2);
//合并集合 將兩個集合中所有元素添加到調用者
[mutableSet2 unionSet:mutableSet3];
NSLog(@"union :%@", mutableSet2);
//刪除指定元素
[mutableSet2 removeObject:@"a"];
NSLog(@"removeObj :%@", mutableSet2);
//刪除所有數據
[mutableSet2 removeAllObjects];
NSLog(@"removeAll :%@", mutableSet2);
//清空接收,把自己清空然后接受另一個set傳過來的所有對象
NSMutableSet *mutableSet4 = [NSMutableSet setWithObjects:@"a",@"b",@"c" ,nil];
[mutableSet4 setSet:set2];
NSLog(@"removeAll :%@", mutableSet4);
/****************? ? Counted Set? ? ****************/
//NSCountedSet類聲明一個可變的編程接口,無序模糊對象的集合。一組數也稱為一個袋子。概述每個不同的對象插入一個NSCountedSet對象有一個與之關聯的計數器。
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithObjects:@"1",@"2",@"3",@"2",@"1", nil];
NSLog(@"countedSet is :%@",countedSet);