IOS集合NSSet与NSMutableSet知识点

NSSet在实际应用中与NSArray区别不大,但是如果你希望查找NSArray中的某一个元素,则需要遍历整个数组,效率低下。而NSSet在查找某一特定的元素的时候则是根据hash算法直接找到此元素的位置,效率高。 NSSet是一个无序的,管理对个对象的集合类,最大特点是集合中不允许出现重复对象,和数学上的集合含义是一样的。除了无序,不许重复,其他功能和NSArray是一样的;需要注意的是:NSSet,NSArray里面只能添加cocoa对象,如果需要加入基本数据类型(int,float,BOOL,double等),需要将数据封装成NSNumber类型。主要是一些常见的操作,别外一些操作见其相应的文档,下面的代码部分还运用的第三方插件BlocksKit相结合;

1:NSSet一些常见的操作

NSSet *newSet=[NSSet setWithObjects:@"wujy",@"cnblogs",@"age",nil];
NSLog(@"set的个数为:%ld",[newSet count]); //不会按上面增加的顺序输入 所以NSSET是没有顺序
NSEnumerator *enumeratorset=[newSet objectEnumerator];
for (NSObject *obj in enumeratorset) {
NSLog(@"key为:%@",obj);
} //是否存在
BOOL isExict=[newSet containsObject:@"wujy"];
NSLog(@"是否存在:%d",isExict); //返回任意一个元素
NSString *anyObje=[newSet anyObject];
NSLog(@"返回任意一个元素:%@",anyObje); //判断是否包含这个元素 并还回
NSObject *memberObj=[newSet member:@"age"];
if (memberObj) {
NSLog(@"存在元素(age):%@",memberObj);
} //简便创建nsset
NSSet *twoSet=[[NSSet alloc] initWithArray:@[@,@,@,@"wujy"]]; //判断两个nsset是否相等
BOOL isEaual=[newSet isEqualToSet:twoSet];
NSLog(@"判断两个nsset是否相等(0):%d",isEaual); //判断两个nsset是否交集
BOOL isInterSects=[newSet intersectsSet:twoSet];
NSLog(@"判断两个nsset是否交集(1):%d",isInterSects); //Blocks NSSet *blockSet=[[NSSet alloc]initWithObjects:@,@,@,@,@,@,nil];
//遍历
[blockSet bk_each:^(id obj) {
NSLog(@"block遍历的值为:%@",obj);
}]; //过滤
NSSet *selectSet=[blockSet bk_select:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"过滤后的nsset(6,5,4):%@",selectSet); //过滤 只在第一个符合条件时就停止
NSSet *matchSet=[blockSet bk_match:^BOOL(id obj) {
BOOL select=[obj intValue]<?YES:NO;
return select;
}];
NSLog(@"matchSet过滤后的nsset(3):%@",matchSet); //取反过滤
NSSet *rejectSet=[blockSet bk_reject:^BOOL(id obj) {
BOOL select=[obj intValue]<?YES:NO;
return select;
}];
NSLog(@"取反过滤后的nsset(6,5,4):%@",rejectSet); //对各个项进行处理
NSSet *mapSet=[blockSet bk_map:^id(id obj) {
return @([obj intValue]+);
}];
NSLog(@"修改后的值为(7,3,6,2,5,4):%@",mapSet); //是否符合条件 返回bool
BOOL isSelected=[blockSet bk_any:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"%d符合条件1",isSelected); //判断是否所有的项都符合这个条件
BOOL allSelected=[blockSet bk_all:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"%d符合条件0",allSelected); //判断是否所有的项都不符合这个条件
BOOL noneSelected=[blockSet bk_none:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"%d符合条件1",noneSelected);

2:NSMutableSet一些常见的操作

    //创建
NSMutableSet *mutableSet=[[NSMutableSet alloc]initWithCapacity:];
[mutableSet addObject:@];
[mutableSet addObjectsFromArray:@[@,@,@]];
NSLog(@"增加后的NSMutableSet(3,2,1,4):%@",mutableSet); //把nsset增加进去
[mutableSet unionSet:[NSSet setWithObjects:@,@,nil]];
NSLog(@"增加后的NSMutableSet(3,6,2,5,1,4):%@",mutableSet); //去除元素
[mutableSet removeObject:@];
NSLog(@"去除后的元素(3,6,2,1,4):%@",mutableSet); //删除nsset里面的
[mutableSet minusSet:[NSSet setWithObjects:@,@,nil]];
NSLog(@"去除后nsset的元素(3,6,4):%@",mutableSet); //nsset转化成nsmutableset
NSSet *newSet=[NSSet setWithObjects:@,@,@, nil];
NSMutableSet *newMutableSet=[NSMutableSet set];
[newMutableSet setSet:newSet];
NSLog(@"转换后的mutableset值为(12,10,11):%@",newMutableSet); //Blocks
//过滤
NSMutableSet *blockMutableSet=[[NSMutableSet alloc]initWithObjects:@,@,@,nil];
[blockMutableSet bk_performSelect:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"过滤后的nsmutableset(25,23):%@",blockMutableSet); //取反过滤
[blockMutableSet bk_performReject:^BOOL(id obj) {
BOOL select=[obj intValue]>?YES:NO;
return select;
}];
NSLog(@"取反过滤后的nsmutableset(23):%@",blockMutableSet); //对项进行修改
[blockMutableSet bk_performMap:^id(id obj) {
return @([obj intValue]+);
}];
NSLog(@"修改后的nsmutableset(24):%@",blockMutableSet);

3:指数集合(索引集合)NSIndexSet

//指数集合(索引集合)NSIndexSet
NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(, )]; //集合中的数字是123 //根据集合提取数组中指定位置的元素
NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSArray * newArray = [array objectsAtIndexes:indexSet]; //返回@"two",@"three",@"four" //可变指数集合NSMutableIndexSet
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
[indexSet addIndex:]
[indexSet addIndex:];
[indexSet addIndex:];
//通过集合获取数组中指定的元素
NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six", nil];
NSArray *newArray = [array objectsAtIndexes:indexSet]; //返回@"one",@"four",@"six"

最近有个妹子弄的一个关于扩大眼界跟内含的订阅号,每天都会更新一些深度内容,在这里如果你感兴趣也可以关注一下(嘿对美女跟知识感兴趣),当然可以关注后输入:github 会有我的微信号,如果有问题你也可以在那找到我;当然不感兴趣无视此信息;

IOS集合NSSet与NSMutableSet知识点

上一篇:BZOJ 3572: [Hnoi2014]世界树


下一篇:The Doors - POJ 1556 (线段相交)