Objective-c 集合對象,objective-c集合

來源:互聯網
上載者:User

Objective-c 集合對象,objective-c集合

  集合(NSSet)是一組單值對象的組合,集合對象的操作包括:搜尋,添加,刪除集合中的成員(可變集合的功能),比較兩個集合,計算兩個集合的交集,並集等。

  下面來看下(NSSet)的方法:

  

 

 

  1)集合的構建

  

 1 // 構建集合的三種方法 2  3 #import <Foundation/Foundation.h> 4  5 int main(int argc , const char *argv[]){ 6     @autoreleasepool { 7         NSSet *set1 = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil]; 8         for(NSString *temp1 in set1){ 9             NSLog(@"temp1 = %@",temp1);10         }11         12         NSArray *array = @[@"aa",@"bb",@"cc"];13         NSSet *set2 = [NSSet setWithArray:array];14         for(NSString *temp2 in set2)15             NSLog(@"temp2 = %@",temp2);16         17         NSSet *set3 = [[NSSet alloc] initWithObjects:@"aa",@"bb"@"cc",nil];18         for(NSString *temp3 in set3)19             NSLog(@"temp3 = %@",temp3);20     }21 }

  2)集合的遍曆

  

 1 #import <Foundation/Foundation.h> 2  3 @interface NSString (print) 4  5 - (void)print; 6 - (void)show:(NSString *)str; 7  8 @end 9 10 @implementation NSString (print)11 12 - (void)print{13     NSLog(@"%@",self);14 }15 - (void)show:(NSString *)str{16     NSLog(@"%@ : %@",str,self);17 }18 19 @end20 int main(int argc , const char *argv[]){21     @autoreleasepool {22         NSSet *set = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil];23         for(NSString *temp in set)24             NSLog(@"temp = %@",temp);25         26         NSLog(@"--------------------------");27         [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {28             NSLog(@"obj = %@",obj);29         }];30         31         NSLog(@"---------------------------");32         [set makeObjectsPerformSelector:@selector(print)];33         34         NSLog(@"----------------------------");35         [set makeObjectsPerformSelector:@selector(show:) withObject:@"this is "];36         37         NSLog(@"-----------------------------");38         NSEnumerator *emr = [set objectEnumerator];39         NSString *temp = nil;40         while(temp = [emr nextObject])41             NSLog(@"temp = %@",temp);42     }43     return 0;44 }

 

 3) 集合的比較

  

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc , const char *argv[]){ 4     @autoreleasepool { 5         NSSet *set = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil]; 6         for(NSString *temp in set) 7             NSLog(@"temp = %@",temp); 8          9         BOOL ishas = [set containsObject:@"lisi"];10         if(ishas)11             NSLog(@"has lisi");12         else13             NSLog(@"no lisi");14         15         NSString *str = [set member:@"aaaa"];16         NSLog(@"str = %@",str);17         18         NSSet *set2 = [set setByAddingObject:@"xiaoliu"];19         NSLog(@"set2 = %@",set2);20         21         NSSet *set3 = [set setByAddingObjectsFromArray:@[@"aa",@"bb",@"cc"]];22         NSLog(@"set3 = %@",set3);23         24         NSSet *set4 = [NSSet setWithObjects:@"zhangsan",@"lisi",nil];25         BOOL issub = [set4 isSubsetOfSet:set];26         if(issub)27             NSLog(@"set4 is set sub class");28         else29             NSLog(@"set4 no set sub class");30         31         BOOL isinterset = [set intersectsSet:set4];32         if(isinterset)33             NSLog(@"set and set4 has intersect");34         else35             NSLog(@"set and set4 no intersect");36         37         BOOL isequal = [set isEqualToSet:set2];38         if(isequal)39             NSLog(@"set = set2");40         else41             NSLog(@"set != set2");42         43     }44     return 0;45 }

 

 

  4)可變集合(NSMutable)

  

 

 

  下面通過一個例子來說可變集合的用法:

  

 1 #import <Foundation/Foundation.h> 2  3 @interface NSString (print) 4 -(void)print; 5 -(void)show:(NSString *)str; 6 @end 7  8 @implementation NSString(print) 9 -(void)print{10     NSLog(@"%@",self);11 }12 -(void)show:(NSString *)str{13     NSLog(@"%@ : %@",str,self);14 }15 @end16 17 int main(int argc,char **argv){18     @autoreleasepool {19         NSMutableSet *mset = [NSMutableSet setWithObjects:@"zhangsan",@"lisi",@"wangwu", nil];20         21         [mset addObject:@"zhaoliu"];22         NSLog(@"mset = %@",mset);23         24         [mset addObjectsFromArray:@[@"111",@"222",@"333"]];25         NSLog(@"mset = %@",mset);26         27         [mset removeObject:@"111"];28         NSLog(@"mset = %@",mset);29         30         NSSortDescriptor *sortdesr = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];31         NSArray *sortset = [mset sortedArrayUsingDescriptors:@[sortdesr]];32         NSLog(@"mset sort = %@",sortset);33         34         NSArray *array = [mset allObjects];35         NSLog(@"array = %@",array);36         NSSet *set2 = [NSSet setWithArray:array];37         NSLog(@"set2 = %@",set2);38         39         NSString *str = [mset anyObject];40         NSLog(@"str = %@",str);41         42         [mset setSet:set2];43         NSLog(@"mset = %@",mset);44         45         [mset removeAllObjects];46         NSLog(@"mset = %@",mset);47     }48 }

 

  

  

  

  

 

 

      

相關文章

聯繫我們

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