Object -C NSSet -- 筆記

來源:互聯網
上載者:User

標籤:

//

//  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;

}

Object -C NSSet -- 筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.