[Objective-C] 010_Foundation架構之NSSet與NSMutableSet

來源:互聯網
上載者:User

標籤:

在Cocoa Foundation中的NSSet和NSMutableSet ,和NSArray功能性質一樣,用於儲存物件屬於集合。但是NSSet和NSMutableSet是無序的, 保證資料的唯一性,當插入相同的資料時,不會有任何效果。

NSSet 初始化及常用操作

#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    NSSet *students = [NSSet setWithObjects:@"小明", @"小輝", @"大雄", nil];    NSSet *teachers = [[NSSet alloc] initWithObjects:@"校長", @"副校長", @"政教主任", nil];    NSArray *array = [NSArray arrayWithObjects:@"小明", @"小輝", @"大雄",@"小李", nil];    NSSet *students_2 = [NSSet setWithArray:array];        NSLog(@"students :%@", students);    NSLog(@"teachers :%@", teachers);    NSLog(@"students_2 :%@", students_2);        //擷取集合students包含對象的個數    NSLog(@"students count :%lu", (unsigned long)students.count);        //以數組的形式擷取集合teachers中的所有對象    NSArray *allTeacher = [teachers allObjects];    NSLog(@"allObj :%@", allTeacher);        //擷取teachers中任意一對象    NSLog(@"anyObj :%@", [teachers anyObject]);        //teachers是否包含某個對象    if ([teachers containsObject:@"副校長"]) {        NSLog(@"teachers中有副校長");    }        //是否包含指定set中的對象    if ([students_2 intersectsSet:students]) {        NSLog(@"intersects");    }        //是否完全符合    if ([students_2 isEqualToSet:students]) {        NSLog(@"完全符合");    }else{        NSLog(@"完全符合? NO。。。。。。。");    }        //是否是子集合    if ([students isSubsetOfSet:students_2]) {        NSLog(@"students isSubsetOf students_2");    }        //迭代器遍曆    NSEnumerator *enumerator = [teachers objectEnumerator];    NSObject *teacher  = [enumerator nextObject];    while (teacher != nil) {        NSLog(@"teachers中的資料: %@",teacher);        teacher = [enumerator nextObject];    }        //快速枚舉遍曆    for (NSObject *teacher in teachers) {        NSLog(@"teachers中的資料: %@",teacher);    }        return YES;}@end

 

NSMutableSet 初始化及常用操作

#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        NSMutableSet *mutableStudent = [NSMutableSet setWithObjects:@"F1", @"F2", @"F3", nil];    NSMutableSet *mutableTeacher = [NSMutableSet setWithObjects:@"B1", @"B2", @"B3", nil];    NSMutableSet *mutableStudent2 = [NSMutableSet setWithObjects:@"F1", @"F2", @"F3",@"F4", nil];        //集合元素相減    [mutableStudent2 minusSet:mutableStudent];    NSLog(@"mutableStudent2 minus mutableStudent:%@", mutableStudent2);        //mutableStudent2隻留下相等元素    [mutableStudent intersectSet:mutableStudent2];    NSLog(@"intersect :%@", mutableStudent2);        //mutableStudent合并集合    [mutableStudent unionSet:mutableStudent2];    NSLog(@"union :%@", mutableStudent);        //mutableTeacher刪除指定元素    [mutableTeacher removeObject:@"好色仙人"];    NSLog(@"removeObj :%@", mutableTeacher);        //mutableTeacher刪除所有資料    [mutableTeacher removeAllObjects];    NSLog(@"removeAll :%@", mutableTeacher);        return YES;}@end

 

 

本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 ) 
轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4623082.html

 

 

[Objective-C] 010_Foundation架構之NSSet與NSMutableSet

相關文章

聯繫我們

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