剛剛學習了Funcdation框架中的NSSet,跟大家分享一下。
1、集合:集合(NSSet)和數組(NSArray)有相似之處,都是存儲不同的對象的地址;不過NSArray是有序的集合,NSSet是無序的集合。
集合是一種哈希表,運用散列算法,查找集合中的元素比數組速度更快,但是它沒有順序。
2、存儲的所有對象只能有唯一一個,不能重復。
1 /**************** Immutable Set ****************/ 2 NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", @"a", @"b", @"c", @"d",nil]; 3 //直接類名調用,不用alloc。+號方法加號方法使用一組對象創建新的集合 4 NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil]; 5 //—號方法 6 NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil]; 7 8 //把數組轉化成集合 9 NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c",@"d", nil]; 10 NSSet *set3 = [NSSet setWithArray:array]; 11 //打印集合中的所有元素 12 NSLog(@"set:%@",set); 13 NSLog(@"set1 :%@", set1); 14 NSLog(@"set2 :%@", set2); 15 NSLog(@"set3 :%@", set3); 16 //獲取集合個數 17 NSLog(@"set count :%lu", set1.count); 18 NSLog(@"set count :%lu", [set1 count]); 19 20 //以數組的形式獲取集合中的所有對象 21 NSArray *allObj = [set3 allObjects]; 22 NSLog(@"allObj :%@", allObj); 23 24 //是否包含某個對象 25 NSLog(@"Is set3 coatains a ? %d", [set3 containsObject:@"a"]); 26 27 //是否包含指定set中的對象 28 NSLog(@"Is set1 contains set3's obj ? %d", [set1 intersectsSet:set3]); 29 30 //是否完全匹配 31 NSLog(@"set1 isEqualto set3 :%d", [set1 isEqualToSet:set3]); 32 NSLog(@"set2 isEqualto set3 :%d", [set2 isEqualToSet:set3]); 33 34 //是否是子集合 35 NSLog(@"set3 isSubSet of set1:%d", [set3 isSubsetOfSet:set1]); 36 NSLog(@"set3 isSubSet of set :%d", [set3 isSubsetOfSet:set ]); 37 38 //set 加一個 arrar 類型的對象 39 NSSet *set4 = [NSSet setWithObjects:@"a", @"b", nil]; 40 NSArray *ary = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]; 41 NSSet *set5 = [set4 setByAddingObjectsFromArray:ary]; 42 NSLog(@"addFromArray :%@", set5); 43 44 //set 加一個 id 類型的對象 45 NSSet *set6 = [set5 setByAddingObject:@"c"]; 46 NSLog(@"set6 is :%@",set6); 47 48 //set 加一個 set 類型的對象 49 NSSet *set7 = [set1 setByAddingObjectsFromSet:set2]; 50 NSLog(@"set7 is :%@",set7); 51 52 /**************** Mutable Set ****************/ 53 54 //初始化 55 56 NSMutableSet *mutableSet = [[NSMutableSet alloc] init]; 57 [mutableSet addObject:@"1"]; 58 59 NSLog(@"mutableSet1 is :%@",mutableSet); 60 //加號方法使用一組對象創建新的集合 61 NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil]; 62 NSLog(@"mutableSet1 is %@",mutableSet1); 63 //初始化一個新分配的集合,大小為size 64 NSMutableSet *mutableSet2 = [[NSMutableSet alloc] initWithCapacity:3]; 65 [mutableSet2 addObject:@"1"]; 66 [mutableSet2 addObject:@"b"]; 67 [mutableSet2 addObject:@"3"]; 68 //創建一個有size大小的新集合 69 NSMutableSet *mutableSet3 = [NSMutableSet setWithCapacity:3]; 70 [mutableSet3 addObject:@"a"]; 71 [mutableSet3 addObject:@"2"]; 72 [mutableSet3 addObject:@"c"]; 73 74 //集合元素相減 75 [mutableSet1 minusSet:mutableSet2]; 76 NSLog(@"minus :%@", mutableSet1); 77 78 //只留下相等元素, 做交集運算 79 [mutableSet2 intersectSet:mutableSet3]; 80 NSLog(@"intersect :%@", mutableSet2); 81 82 //合並集合 將兩個集合中所有元素添加到調用者 83 [mutableSet2 unionSet:mutableSet3]; 84 NSLog(@"union :%@", mutableSet2); 85 86 //刪除指定元素 87 [mutableSet2 removeObject:@"a"]; 88 NSLog(@"removeObj :%@", mutableSet2); 89 90 //刪除所有數據 91 [mutableSet2 removeAllObjects]; 92 NSLog(@"removeAll :%@", mutableSet2); 93 94 //清空接收,把自己清空然后接受另一個set傳過來的所有對象 95 NSMutableSet *mutableSet4 = [NSMutableSet setWithObjects:@"a",@"b",@"c" ,nil]; 96 [mutableSet4 setSet:set2]; 97 NSLog(@"removeAll :%@", mutableSet4); 98 99 /**************** Counted Set ****************/ 100 101 //NSCountedSet類聲明一個可變的編程接口,無序模糊對象的集合。一組數也稱為一個袋子。概述每個不同的對象插入一個NSCountedSet對象有一個與之關聯的計數器。 102 103 104 NSCountedSet *countedSet = [[NSCountedSet alloc] initWithObjects:@"1",@"2",@"3",@"2",@"1", nil]; 105 106 NSLog(@"countedSet is :%@",countedSet);