zoukankan      html  css  js  c++  java
  • 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 @end
    20 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         else
    13             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         else
    29             NSLog(@"set4 no set sub class");
    30         
    31         BOOL isinterset = [set intersectsSet:set4];
    32         if(isinterset)
    33             NSLog(@"set and set4 has intersect");
    34         else
    35             NSLog(@"set and set4 no intersect");
    36         
    37         BOOL isequal = [set isEqualToSet:set2];
    38         if(isequal)
    39             NSLog(@"set = set2");
    40         else
    41             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 @end
    16 
    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 }

      

      

      

      

          

  • 相关阅读:
    sed命令详解 皇星客栈
    FD_set FD_zero 皇星客栈
    linux tr命令详解 皇星客栈
    解决中文乱码问题(Ubuntu) 皇星客栈
    FIFO深度 皇星客栈
    输入阻抗 皇星客栈
    APROM Data Flash LDROM 皇星客栈
    kubernetes 1.21 部署业务
    kubernetes 1.21部署 cephcsi rbd
    kubernetes 1.21 部署 dashboard
  • 原文地址:https://www.cnblogs.com/xiaozhai-1234/p/5268256.html
Copyright © 2011-2022 走看看