天天看點

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;

}