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];
//集合的创建
//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];
相关文章推荐
- NSSet和NSMutableSet 确保数据的唯一性
- NSArray(二) 、 NSMutableArray 、 NSSet 、 NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- NSSet和NSMutableSet
- iOS开发-Day19-OC NSDictionary&NSMutableDictionary&NSSet&NSMutableSet
- NSSet和NSMutableSet介绍
- 黑马程序员——Objective-C基础—— NSSet & NSMutableSet
- 21 Foundation框架之NSSet、NSMutableSet、NSCountedSet、NSOrderedSet、NSMutableOrderedSet
- NSSet和NSMutableSet - By吴帮雷
- Objective-C语法之NSSet和NSMutableSet
- NSSet、NSMutableSet基本用法
- Objective-c的Foundation中常用类5——集合中的NSSet、NSMutableSet
- Foundation 之 NSSet和NSMutableSet以及NSIndexSet和NSMutableIndexSet
- NSSet,NSMutableSet,NSIndexSet
- 10,Objective-C语法之NSSet和NSMutableSet
- Foundation中的NSSet和NSMutableSet
- [ios] NSSet,NSMutableSet[zhuan]
- OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet
- NsSet与NSMutableSet
- NSSet和NSMutableSet