標籤:
NSArray 有序的 自然順序
NSSet 無序的
NSSet 中不能儲存重複的資料,可以用它來去除重複的資料
1.建立集合
1.1建立不可變集合
1 NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three", nil];2 NSLog(@"%@",set);3 4 NSSet * set1 = [[NSSet alloc] initWithObjects:@"one",@"two",@"one",@"three", nil];5 NSLog(@"%@",set1);
1.2建立可變集合
1 NSMutableSet * muset = [[NSMutableSet alloc] initWithObjects:@"one",@"two", nil];2 3 NSLog(@"%@",muset);
2.擷取元素個數
1 NSUInteger count = [set1 count]; 2 NSLog(@"%lu",count);
3.判斷是否包含對象
1 BOOL isContain = [set1 containsObject:@"T"];2 if (isContain) {3 NSLog(@"包含");4 } else {5 NSLog(@"不包含");6 }
- (BOOL)containsObject:(ObjectType)anObject;
4.數群組轉換為集合
1 NSArray * arr = @[@"one",@"two",@"three",@"one"];2 NSSet * set2 =[NSSet setWithArray:arr];3 NSLog(@"set2 %@",set2);
5. 可變數組的增刪
5.1 添加
1 [muset addObject:@"three"]; 2 NSLog(@"%@",muset);
5.2 刪除
1 [muset removeObject:@"one"]; 2 NSLog(@"%@",muset);
刪除全部
1 [muset removeAllObjects]; 2 NSLog(@"%@",muset);
Objective-C集合初識