NSSet 集合建立,獲取,遍歷,可變集合的刪除
阿新 • • 發佈:2019-02-12
NSSet 集合
- NSArray 自然順序
- NSSet是無序的
- 注意:這個是最為重要的功能 NSSet 中不能夠儲存重複的資料,可以用它來去除重複的值
NSString * str1 = @"one";
NSString * str2 = @"two";
NSString * str3 = @"three";
NSSet * set = [[NSSet alloc] initWithObjects:str1,str2,str3,str1, nil];
NSLog(@"set %@",set);
//count
NSLog(@"count %ld",set.count);
OOL isContains = [set containsObject:str1];
if (isContains)
{
NSLog(@"YES");
}
else
{
NSLog(@"NO");
}
遍歷
NSEnumerator * enumerator = [set objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
NSMutableSet 可變集合
NSString * str1 = @"one";
NSString * str2 = @"two";
//1 建立一個可變集合
NSMutableSet * muSet = [[NSMutableSet alloc] init];
//2.增加值
[muSet addObject:str1];
[muSet addObject:str2];
NSLog(@"muSet %@",muSet);
//3.刪除
[muSet removeObject:str1];
NSLog(@"muSet %@",muSet);
//4.刪除所有
[muSet removeAllObjects];
NSLog(@"muSet %@",muSet);
//5.遍歷
NSEnumerator * en = [muSet objectEnumerator];
NSString * value;
while (value = [en nextObject]) {
NSLog(@"value %@",value);
}