第八天 NSSet集合
NSSet集合的好处有两点第一就是元素是不可以重复2是无序的
eg
NSArray *array=@[@"one",@"two",@"three"];
NSLog(@"%@",array);
NSSet *set=[NSSet setWithArray:array];
NSLog(@"%@",set);
NSSet 的创建
NSSet *set=[NSSet setWithObject :@"one",@"two",@"three",nil];
NSLog(@"%@",set);
另外一种创建
NSSet *set2=[NSSet setWithSet:set1];
NSLog(@"%@",se2);
使用另外一个数组创建
NSArray *array=@[@"one",@"two",@"trhee"];
NSSet *set3=[NSSet setWithArray:array];
NSLog(@"%@",set3);
枚举遍历
NSSet *set1=[NSSet setWithObjects:@"one",@"two",@"three"];
NSEnumerator *e=[set1 objectEnumerator];
NSString *s;
while((s=[e nextObject])!=nil)
{
NSLog(@"%@",s);
判断某个对象中是否有其他成员
NSSet *set1=[NSSet setWithObjects:@"one",@"two",@"three"];
NSSting *e=[set1 member:@"one"];
NSLog(@"%@",e);
}
获取集合中的任意一个对象
NSSet *set1=[NSSet setWithObjects :@"one",@"two",@"three"];
NSString *e=[set1 anyObject];
NSLog(@"%@",e);
判断两个Set是否相等
NSSet *set1=[NSSet setWithObjects:@"one",@"two",@"one"];
NSSet *set2=[NSSet setWithObjects:@"one",@"two",@"one"];
BOOL r=[set1 isEqualToSet:set2];
NSLog(@"%@",r==1?@"YES":@"NO");
//判断两个集合是否有交集
NSSet *set1=[NSSet setWithObjects:@"one",@"two",@"one",@"100",nil];
NSSet *set2=[NSSet setWithObjects:@"one",@"two",@"one",@"200",nil];
BOOL r=[set1 isSubsetOfSet:set2];
NSLog(@"%@",r==1?@"YES":@"NO");

浙公网安备 33010602011771号