Objective-C:NSSet和NSMutbaleSet的用法

來源:互聯網
上載者:User

標籤:


    1. #import <Foundation/Foundation.h>      
    2. int main(int argc, const char * argv[])  
    3. {  
    4.   
    5.     @autoreleasepool {  
    6.           
    7.         NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];  
    8.         NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil];  
    9.         NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];  
    10.         NSSet *set3 = [NSSet setWithArray:array];  
    11.           
    12.         NSLog(@"set1 :%@", set1);  
    13.         NSLog(@"set2 :%@", set2);  
    14.         NSLog(@"set3 :%@", set3);  
    15.           
    16.         //擷取集合個數  
    17.         NSLog(@"set1 count :%d", set1.count);  
    18.           
    19.         //以數組的形式擷取集合中的所有對象  
    20.         NSArray *allObj = [set2 allObjects];  
    21.         NSLog(@"allObj :%@", allObj);  
    22.           
    23.         //擷取任意一對象  
    24.         NSLog(@"anyObj :%@", [set3 anyObject]);  
    25.           
    26.         //是否包含某個對象  
    27.         NSLog(@"contains :%d", [set3 containsObject:@"obj2"]);  
    28.           
    29.           
    30.         //是否包含指定set中的對象  
    31.         NSLog(@"intersect obj :%d", [set1 intersectsSet:set3]);  
    32.           
    33.         //是否完全符合  
    34.         NSLog(@"isEqual :%d", [set2 isEqualToSet:set3]);  
    35.           
    36.         //是否是子集合  
    37.         NSLog(@"isSubSet :%d", [set3 isSubsetOfSet:set1]);  
    38.           
    39.           
    40.           
    41.         NSSet *set4 = [NSSet setWithObjects:@"a", @"b", nil];  
    42.         NSArray *ary = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];  
    43.         NSSet *set5 = [set4 setByAddingObjectsFromArray:ary];  
    44.         NSLog(@"addFromArray :%@", set5);  
    45.           
    46.           
    47.           
    48.           
    49.         NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];  
    50.         NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"a", @"2", @"b", nil];  
    51.         NSMutableSet *mutableSet3 = [NSMutableSet setWithObjects:@"1", @"c", @"b", nil];  
    52.           
    53.         //集合元素相減  
    54.         [mutableSet1 minusSet:mutableSet2];  
    55.         NSLog(@"minus :%@", mutableSet1);  
    56.           
    57.         //只留下相等元素  
    58.         [mutableSet1 intersectSet:mutableSet3];  
    59.         NSLog(@"intersect :%@", mutableSet1);  
    60.           
    61.         //合并集合  
    62.         [mutableSet2 unionSet:mutableSet3];  
    63.         NSLog(@"union :%@", mutableSet2);  
    64.           
    65.         //刪除指定元素  
    66.         [mutableSet2 removeObject:@"a"];  
    67.         NSLog(@"removeObj :%@", mutableSet2);  
    68.           
    69.           
    70.         //刪除所有資料  
    71.         [mutableSet2 removeAllObjects];  
    72.         NSLog(@"removeAll :%@", mutableSet2);  
    73.           
    74.     }  
    75.     return 0;  
    76. }  

Objective-C:NSSet和NSMutbaleSet的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.