NSSet,NSMutableSet,NSIndexSet

NSArray:有序的集合,NSSet:无序的集合,散列存储。 但是NSSet保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表。NSMutableSet是NSSet的子类,是NSSet的可变形式。

NSSet、NSMutableSet

NSSet的使用
[NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造
[NSSet setWithArray:(NSArray *)array];用数组构造
[NSSet setWithObjects:…]:创建集合对象,并且初始化集合中的数值,结尾必需使用nil标志。

[set count] ; 得到这个结合对象的长度。
[set containsObject:…]: 判断这个集合中是否存在传入的对象,返回Bool值.

-(BOOL)member:obj;使用isEqual:方法确定集合是否包含obj

[set objectEnumerator]: 为集合中所有对象返回一个NSEnumerator对象。
[enumerator nextObject]:得到迭代器中的下一个节点数据,使用while遍历这个迭代器,方可遍历集合对象中的对象。
[set isEqualToSet:nsset]:判断两个集合是否完全相等,返回Bool值。
[set isSubsetOfSet:nsset]:判断集合中的所有数据是否都在nsset集合中,返回Bool值。
-(BOOL)intersectSet:nsset;确定接受者中是否至少有一个成员出现在nsset中

NSMutableSet继承NSSet,它可以使用NSSet的方法。

[NSMutableSet setWithCapacity:6]:创建可变集合对象,并且初始化长度为6。
[set addObject: obj] : 向集合中动态的添加对象。
[set removeObject:obj]:删除集合中的一个对象。
[set removeAllObjects]:删除集合中的所有对象。
[set unionSet:obj]:向集合中添加一个obj集合的所有数据。
[set minusSet:obj]:向集合中删除一个obj集合的所有数据。
[set intersectSet]:向集合中删除一个不属于obj集合的所有数据。

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //定义一个NSSet
        NSSet* set1 = [NSSet setWithObjects:@"str1",@"str2",@"str3", nil];

        //使用NSArray初始化一个NSSet
        NSArray *array = [[NSArray alloc] initWithObjects:@"obj1",@"obj2", @"obj3",@"str1",nil];
        NSSet* set2 = [NSSet setWithArray:array];

        //用另外一个set对象构造
        NSSet* set3 = [NSSet setWithSet:set2];

        //可变长度的集合
        NSMutableSet* set4 = [NSMutableSet setWithSet:set3];

        //在可变集合中添加和移除对象
        [set4 addObject:@"obj4"];
        [set4 removeObject:@"obj2"];

        //获取这个结合对象的长度
        NSLog(@"size of set1:%lu",[set1 count]);

        //判断是否含有某个对象
        if ([set3 containsObject:@"str1"])
            NSLog(@"set3 包含 str1");
         else
            NSLog(@"set3 不包含 str1");

        //判断set1 是否等于set2
        if ([set1 isEqualToSet:set2]) 
            NSLog(@"set1 等于 set2");
        else
            NSLog(@"set1 不等于 set2");

        //判断set1 是否是set2的子集合
        if ([set1 isSubsetOfSet:set2])
            NSLog(@"set1 是 set2的子集合");
        else
            NSLog(@"set1 不是 set2的子集合");

        //获取两个集合的交集
        [set1 intersectsSet:set2];

        //获取两个集合的并集
        [set4 unionSet:set3];

        //迭代遍历
        for (NSObject* object in set4) {
             NSLog(@"set4里的对象:%@", object);
        }

        //使用NSEnumerator迭代遍历
        NSEnumerator *enumerator = [set4 objectEnumerator];
        for (NSObject *object in enumerator) {
            NSLog(@"set4里的对象:%@", object);
        }
    }
    return 0;
}

 

 

NSIndexSet

NSIndexSet类和它的可变副本(NSMutableIndexSet)表示一个唯一的无符号整数的集合。这个类用于存储有序的索引到某种数据结构。例如,给定一个NSArray对象,你可以使用该数组中的索引,以确定对象的一个子集。

NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];

NSMutableIndexSet *indexSet2 = [NSMutableIndexSet indexSet];
[indexSet addIndex:2];
[indexSet addIndex:5];
[indexSet addIndex:8];

//列出NSIndexSet的值
unsigned index;
for (index = [indexSet firstIndex];
	 index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])  {
	
}

NSIndexSet的一些方法

+(NSIndexSet) indexSet   创建一个空的索集合
-(BOOL) containIndex:idx 如果索引集合包含索引idx,则返回YES,否则返回NO
-(NSUinteger) count 返回索引集合中索引的数量
-(NSUinteger) firstIndex 返回集合中的第一个索引,如何集合为空,则返回NSNotFound
-(NSUinteger) indexLessThanIndex:idx 返回集合中小于idx的最接近的索引,如果没有小于idx的索引,则返回NSNotFound.类似indexLessOrEuqalToIndex:、indexGreaterThanIndex和indexGreaterThanOrEqualIndex:
-(NSIndexSet *) indexesPassingTest:(BOOL)(^)(NSUinteger idx,BOOL *stop) block  区块应用在集合中的每个元素。idx添加到了NSIndexSet中返回YES,否则返回NO。设置指针变量stop为YES,表示中断处理。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值