集合的概念
指具有某种特定性质的对象汇总成的集体。这些对象叫集合元素。
比如:整数集{-1,0,1,2}、自然数集{1,2,3,4,5}
- 特点: 确定性 互异性 无序性
Foundationframework中对象(NSObject)的三种集合
NSSet |
NSMutableSet |
用于对象无序集合(集合) |
NSArray |
NSMutableArray |
用于对象有序集合(数组) |
NSDictionary |
NSMutableDictionary |
用于键值映射(字典) |
注:这些集合类只能容纳
cocoa
对象(NSOjbect
对象)
集合类如何保存基本数据类型
例如:int,float, double, BOOL,struct
等。
需要将这些基本数据封装成NSNumber
类型进行存储。NSNumber
对象是cocoa
对象,这样就可以被保存在集合类中。
NSNumber封装对象
-(NSNumber* )initWithFloat:(float)value;
-(NSNumber* )initWithDouble:(double)value;
-(NSNumber* )initWithBool:(BOOL)value ;
-(NSNumber* )initWithInteger:(NSInteger)value;
NSSet, NSMutableSet
1 集合对象唯一(2个相同对象,无法同时存在)
2 通过hash查询快速,但是无序,杂乱
3 不能确定加入集合后对象的位置
4 数据量大的场合,数据筛选及遍历,建议使用
集合NSSet声明
对象方法-initWithObjects
NSSet * set = [[NSSet alloc] initWithObjects:@"one”, nil];
类方法-Set
NSSet * set = [NSSetSet];
集合NSMutableSet
1 因NSMutableSet继承NSSet,拥有NSSet方法
2 新增修改,删除,追加函数
集合操作
集合的创建
NSSet *set1 = [NSSet setWithObjects:@"1",@"2", nil];
NSSet *set2 = [NSSet setWithObjects:@"3",@"4",@"5",@"6", nil];
NSArray *array = [NSArray arrayWithObjects:@"6",@"7",@"8", nil];
NSSet *set3 = [NSSet setWithArray:array];
NSSet *set4 = [NSSet setWithSet:set1];
NSSet *set5 = [NSSet setWithObjects:@"1",@"2", nil];
NSSet *set6 = [NSSet setWithObjects:@"1",@"2",@"6",@"7", nil];
返回数量
NSInteger count = [set3 count];
NSLog(@"set3 count = %ld",(long)count);
返回任意对象
NSString *string = [set3 anyObject];
NSLog(@"string = %@",string);
返回所有对象
NSArray *objects = [set3 allObjects];
NSLog(@"objects = %@",objects);
是否包含
BOOL isContain = [set3 containsObject:@"6"];
BOOL isContain1 = [set3 containsObject:@"10"];
NSLog(@"isContain = %d, isContain1 = %d",isContain, isContain1);
集合间是否存在交集
BOOL isIntersect = [set2 intersectsSet:set3];
BOOL isInterset1 = [set1 intersectsSet:set2];
NSLog(@"isItersect = %ld,isItersect1 = %ld",(long)isIntersect,(long)isInterset1);
集合是否匹配
BOOL isEqual = [set1 isEqualToSet:set2];
BOOL isEqual1 = [set1 isEqualToSet:set5];
NSLog(@"isEqual = %ld,isEqual1 = %ld",(long)isEqual,(long)isEqual1);
结合是否是字子集
BOOL isSub = [set2 isSubsetOfSet:set3];
bool isSub1 = [set1 isSubsetOfSet:set6];
NSLog(@"isSub = %ld, isSub1 = %ld",(long)isSub, (long)isSub1);
集合的添加
NSSet *set7 = [set5 setByAddingObject:@"One"];
NSLog(@"set7 = %@",set7);
NSArray *array1 = [NSArray arrayWithObjects:@"two",@"three",@"four", nil];
NSSet *set8 = [set7 setByAddingObjectsFromArray:array1];
NSLog(@"set8 = %@",set8);
NSSet *set9 = [set8 setByAddingObjectsFromSet:set5];
NSLog(@"set9 = %@",set9);
可变集合
NSMutableSet *mutableSet1 = [NSMutableSet set];
NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"1",@"a", nil];
NSMutableSet *mutableSet3 = [NSMutableSet setWithObjects:@"2",@"a", nil];
NSMutableSet *mutableSet4 = [NSMutableSet setWithObjects:@"2",@"a",@"b", nil];
NSMutableSet *mutableSet5 = [NSMutableSet setWithObjects:@"2",@"a",@"b",@"c",@"d", nil];
NSLog(@"mutableSet1 = %@",mutableSet1);
NSLog(@"mutableSet2 = %@",mutableSet2);
NSLog(@"mutableSet3 = %@",mutableSet3);
一个集合减去另一个集合的元素
[mutableSet2 minusSet:mutableSet3];
NSLog(@"mutableSet2 = %@",mutableSet2);
两个集合获取交集部分
[mutableSet2 intersectSet:mutableSet3];
NSLog(@"mutableSet2 = %@",mutableSet2);
获取两个集合的并集
[mutableSet2 unionSet:mutableSet3];
NSLog(@"mutableSet2 = %@",mutableSet2);
一个集合给另一个集合赋值
[mutableSet1 setSet:mutableSet3];
NSLog(@"mutableSet1 = %@",mutableSet1);
删除集合中的对象
NSLog(@"mutableSet4 = %@",mutableSet4);
[mutableSet4 removeObject:@"b"];
NSLog(@"mutableSet4 = %@",mutableSet4);
[mutableSet4 removeAllObjects];
NSLog(@"mutableSet4 = %@",mutableSet4);
使用数组的方式进行添加对象
NSLog(@"mutableSet5 = %@",mutableSet5);
NSArray *array2 = [NSArray arrayWithObjects:@"e",@"f", nil];
[mutableSet5 addObjectsFromArray:array2];
NSLog(@"mutableSet5 = %@",mutableSet5);