天天看点

OC-NSSet

//

//  main.m

//  NSset_new

//

//  Created by qianfeng on 14-11-9.

//  Copyright (c) 2014年 qianfeng. All rights reserved.

//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {

        // insert code here...

        NSLog(@"Hello, World!");

        //集合没有顺序,查找速度快

        //哈希表,散列算法,

        //可以存储任何类型的对象,但是没有顺序,输出无顺序,

        NSSet *set = [[NSSet alloc] initWithObjects:@"one",@"tow",@"three", nil];

        NSLog(@"%@ \n lenth = %d",set,(int)[set count]);

        BOOL ret = [set containsObject:@"five"];

        NSLog(@"is in :%d",ret);

        NSSet *set1 = [[NSSet alloc] initWithObjects:@"one",@"tow",@"three",@"six",@"nine", nil];

        BOOL ret1 = [set isEqual:set1];

        NSLog(@"is quer :%d",ret1);

        //集合不会存储相同的两个元素

        //判断set是否为set2的子集,如果在set再添加n个“one”,set仍然是set2的子集,原因是集合不存储相同与元素。

        BOOL ret3 = [set isSubsetOfSet:set1];

        NSLog(@"is kid:%d",ret3);

        //使用枚举遍历集合

        //通过数组生成集合:

        NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"one",@"one", nil];

         NSSet *set3 = [[NSSet alloc] initWithArray:array];

        NSLog(@"%@",set3);

//        通过集合生成数组:

        //可变集合:

        NSMutableSet *muset = [[NSMutableSet alloc] init];

        [muset addObject:@"one"];

        [muset addObject:@"tow"];

        [muset addObject:@"three"];

        [muset addObject:@"four"];

        [muset addObject:@"five"];

        NSLog(@"%@",muset);

        [muset removeObject:@"one"];

        NSLog(@"%@",muset);

        NSMutableSet *muset2 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil];

        NSLog(@"%@",muset2);

        //把nuset中的元素拷贝到muset2中,期间如果遇到重复则不拷贝,

        [muset2 unionSet:muset];

        NSLog(@"%@",muset2);

        //从muset2中删除和muset的元素。

        [muset2 minusSet:muset];

        NSLog(@"%@",muset2);

        //NEW:::::::::

        //指数集合,索引集合

//        NSIndexSet,NSMutableIndexSet

        NSIndexSet *index = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)];

        //index里面的数据是1,2,3

        NSLog(@"index = %@",index);

        //通过截取连续数字:

        NSArray *newarray = [[NSArray alloc] initWithObjects:@"one",@"a",@"b",@"b",@"c", nil];

        NSArray *ary = [newarray objectsAtIndexes:index];

        //截取的数字是从下标为1的元素起,往后取3个(包括下标为1的元素)

        NSLog(@"ary = %@",ary);

//可变指数集合:

        NSMutableIndexSet *muindexSet = [[NSMutableIndexSet alloc] init];

        //使用可变索引,截取不连续元素。

        [muindexSet addIndex:0];

        [muindexSet addIndex:2];

        [muindexSet addIndex:4];

        NSArray *newarray_1 = [[NSArray alloc] initWithObjects:@"one",@"a",@"b",@"b",@"c", nil];

        NSArray *ary_2 = [newarray_1 objectsAtIndexes:muindexSet];

        NSLog(@"ary_2 = %@",ary_2);

    }

    return 0;

}