Objective-C集合初識

來源:互聯網
上載者:User

標籤:

 NSArray 有序的 自然順序

 NSSet 無序的

 NSSet 中不能儲存重複的資料,可以用它來去除重複的資料

1.建立集合

  1.1建立不可變集合

    

1     NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three", nil];2     NSLog(@"%@",set);3     4     NSSet * set1 = [[NSSet alloc] initWithObjects:@"one",@"two",@"one",@"three", nil];5     NSLog(@"%@",set1);

  1.2建立可變集合

1         NSMutableSet * muset = [[NSMutableSet alloc] initWithObjects:@"one",@"two", nil];2         3         NSLog(@"%@",muset);

 

2.擷取元素個數

 1 NSUInteger count = [set1 count]; 2 NSLog(@"%lu",count); 

3.判斷是否包含對象

1     BOOL isContain = [set1 containsObject:@"T"];2     if (isContain) {3         NSLog(@"包含");4     } else {5         NSLog(@"不包含");6     }

- (BOOL)containsObject:(ObjectType)anObject;

 

4.數群組轉換為集合

1     NSArray * arr = @[@"one",@"two",@"three",@"one"];2     NSSet * set2 =[NSSet setWithArray:arr];3     NSLog(@"set2 %@",set2);

 

5. 可變數組的增刪

  5.1 添加

 1 [muset addObject:@"three"]; 2 NSLog(@"%@",muset); 

  5.2 刪除

 1 [muset removeObject:@"one"]; 2 NSLog(@"%@",muset); 

    刪除全部

 1 [muset removeAllObjects]; 2 NSLog(@"%@",muset); 

 

Objective-C集合初識

聯繫我們

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