Objective-c set object, objective-c set

Source: Internet
Author: User

Objective-c set object, objective-c set

A collection (NSSet) is a combination of Single-value objects. operations on a set object include: search, add, and delete members in the Set (the function of a variable set), and compare the two sets, calculates the intersection and union of two sets.

Next let's take a look at the (NSSet) method:

  

 

 

1) Build a set

  

1 // three methods for building a set 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) traversal of a set

  

 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) Comparison of Sets

  

 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) variable set (NSMutable)

  

 

 

The following example shows how to use a variable set:

  

 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 }

 

  

  

  

  

 

 

      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.