您的位置:首页 > 其它

NSSet与NSMutableSet

2012-12-25 21:44 344 查看
NSSet-----------------------------------------------------------------------

//集合的创建
//NSSet *set1 = [NSSet setWithObjects:@"1",@"3",@"2",nil];
NSSet *set2 = [[NSSet
alloc] initWithObjects:@"3",@"4",@"5",nil];
NSArray *array1 = [NSArray
arrayWithObjects:@"1",@"3",@"5",nil];
NSSet *set3 = [NSSet
setWithArray:array1];
NSSet *set4 = [NSSet
setWithSet:set1];

//集合中对象的个数
int count = [set1
count];

//返回集合中所有的对象
NSArray *array = [set1
allObjects];

//返回集合中任意一个对象
id object1 = [set1
anyObject];

//集合1中是否包含内容为a的值,是返回YES,否则返回NO
BOOL bool1 = [set1
containsObject:@"a"];

//集合2是否与集合1中的内容完全匹配,是返回YES,否则返回NO
BOOL bool2 = [set2
isEqualToSet:set1];

//集合2与集合1是否存在交集,是返回YES,否则返回NO
BOOL bool3 = [set2
intersectsSet:set1];

//集合2是否是集合1的子集
BOOL bool4 = [set2
isSubsetOfSet:set1];

//创建一个新的集合,原set5是不发生改变的
NSSet *set5 = [NSSet
setWithObjects:@"one",nil];
NSSet *appSet1 = [set5
setByAddingObject:@"two"];
NSSet *appSet2 = [set5
setByAddingObjectsFromSet:set1];
NSSet *appSet3 = [set5
setByAddingObjectsFromArray:array1];

//NSMutableSet--------------------------------------------------------------

//创建一个可变的集合
NSMutableSet *mSet1 = [NSMutableSet
set];
NSMutableSet *mSet2 = [NSMutableSet
setWithObjects:@"1",@"2",nil];
NSMutableSet *mSet3 = [NSMutableSet
setWithObjects:@"3",@"2",nil];
NSMutableSet *mSet4 = [NSMutableSet
setWithObjects:@"1",@"2",nil];
NSMutableSet *mSet5 = [NSMutableSet
setWithObjects:@"3",@"2",@"1",nil];

//添加指定的数组数据
NSArray *array = [NSArray
arrayWithObjects:@"5",@"6",nil];
[mSet5
addObjectsFromArray:array];

//取集合3与集合2的并集
[mSet3
unionSet:mSet2];//mSet3=3,2,1

//取集合3与集合2的交集
[mSet3
intersectSet:mSet2];//mSet3=2

//集合3减去与集合2中相同的内容
[mSet3
minusSet:mSet2];//mSet3=3

//删除集合中指定的数据
[mSet4
removeObject:@"1"];

//删除全部数据
[mSet5
removeAllObjects];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NSMutableSet NSSet