【iOS】NSSet 集合创建,获取,遍历,可变集合的删除

版权声明:本文为博主原创,如需转载请注明出处。

NSSet 集合

  • NSArray 自然顺序
  • NSSet是无序的
  • 注意:这个是最为重要的功能 NSSet 中不能够存储重复的数据,可以用它来去除重复的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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");
}

遍历

1
2
3
4
5
NSEnumerator * enumerator = [set objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}

NSMutableSet 可变集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
}

新博客文章地址:NSSet 集合创建,获取,遍历,可变集合的删除
CSDN文章地址:NSSet 集合创建,获取,遍历,可变集合的删除