Object -C NSSet -- 笔记

//

//  main.m

//  NSSET

//

//  Created by facial on 25/8/15.

//  Copyright (c) 2015 facial_huo. 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", @"two", @"three", @"four", nil];

        

        //打印set 获取nsset的长度

        NSLog(@"%@, %lu", set, [set count] );

        

        bool ret = [set containsObject: @"one"];

        NSLog(@"%d", ret);

        

        

        //判断两个集合是否相等

        

        NSSet *set2 = [[NSSet alloc] initWithObjects: @"one", @"two", @"three", @"four", @"five",nil];

        bool isSame = [ set isEqualToSet: set2];

        NSLog(@"%d", isSame);

        

        

        //判断是否是子集合

        

        bool isSub = [set isSubsetOfSet: set2];

        NSLog(@"%d", isSub);

        

        

        //枚举器 遍历nsset元素

        NSEnumerator *enumor = [set objectEnumerator];

        NSString *item;

        while (item = [enumor nextObject]) {

            NSLog(@"%@",item);

        }

        

        

        //通过数组创建集合

        

        NSArray *array = [[NSArray alloc] initWithObjects: @"arry1", @"arry2", @"arry3"nil];

        

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

        

        NSLog(@"%@", arraySet);

        

        //把集合变成数组

        

        NSArray *SetToArray = [arraySet allObjects];

        NSLog(@"%@", SetToArray);

        

        

        // NSMutableSet; 添加元素

        

        NSMutableSet *mSet = [NSMutableSet new];

        [mSet addObject: @"a"];

        [mSet addObject: @"b"];

        [mSet addObject: @"c"];

        

        NSLog(@"%@", mSet);

        

        

        //删除元素

        

        [mSet removeObject: @"a"];

        NSLog(@"%@", mSet);

        

        // 把一个集合添加到另外一个集合

        

        NSSet *test_set = [[NSSet alloc] initWithObjects: @"d", @"e", @"f", nil];

        

        [mSet unionSet: test_set];

        NSLog(@"%@", mSet);

        

        //取两个集合的交集

        

        NSSet *test_set2 =  [[NSSet alloc] initWithObjects: @"a", @"b", @"f", nil];

        

        [mSet minusSet: test_set2];

        

        NSLog(@"%@", mSet);

        

        //索引集合

        

        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange(2, 3)];

        NSArray *array2 = [[NSArray alloc] initWithObjects: @"a", @"b", @"c",@"d", @"e", nil];

    

        NSArray *newArray2 = [array2 objectsAtIndexes: indexSet];

        

        NSLog(@"%@", newArray2);

        

        

        

        //

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

        [muteIndex addIndex: 0];

        [muteIndex addIndex: 1];

        

        NSArray *newArray3 = [[NSArray alloc] initWithObjects: @"aa", @"bb", @"cc", @"dd", @"ee", nil];

        

        NSArray *newArray4 = [newArray3 objectsAtIndexes: muteIndex];

        

        

        //NSArray *a = [array2 objectAtIndex: index];

         NSLog(@"%@", newArray4);

        

        

        

 

        

    }

    return 0;

}

posted @ 2015-08-26 21:21  facial  阅读(155)  评论(0编辑  收藏  举报