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);
//打印个数
NSLog(@"count %@",set.count);
//判断set中是否存在某个对象
BOOL 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 *muSet = [[NSMutableSet alloc]init];
//添加对象
NSString *str1 = @"one”;
NSString *str2 = @"two”;
[muSet addObject:str1];[muSet addObject:str2];
//删除
[muSet removeObject:str1];
//删除所有
[muSet removeAllObject];
//遍历
NSEnumerator *enumerator = [muSet objectEnumerator];
NSString *value;
while(value = [enumerator nextObject])
{
NSLog(@" %@",value);
}