zoukankan      html  css  js  c++  java
  • 字符串、集合补充

    1.数组与字符间转换

    componentsSeparatedByString:将字符串以x分割成数组

    componentsJoinedByString:将数组以x拼接为字符串

    2.排序

    Description:按描述条件(“一般字符对象”或对象的属性)排序.

    注意可变排序方法和不可排序方法的是否有返还值.

        NSString *str = @"123-456-789-000";
        NSArray *arr = [str componentsSeparatedByString:@"-"];
        NSLog(@"%@",arr);
        NSString *str2 = [arr componentsJoinedByString:@""];//数组转为字符串
        NSLog(@"%@",str2);
        
        NSMutableArray *arr2 = [[NSMutableArray alloc]init];
        [arr2 addObject:@"1"];
        [arr2 insertObject:@"3" atIndex:0];
        [arr2 addObject:@"2"];
        NSLog(@"%@",arr2);
        //系统的compara:适合字符串,不适合对象排序
        //2
        NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];//nil或self
        [arr2 sortUsingDescriptors:[NSArray arrayWithObject:sort1]];//注意可变排序方法和不可排序方法的区别
        
        NSLog(@"%@",arr2);
        //3复杂对象的排序
        Student *s1 = [[Student alloc]init];
        s1.name = @"obanma";
        
        Student *s2 = [[Student alloc]init];
        s2.name = @"jobs";
        
        Student *s3 = [[Student alloc]init];
        s3.name = @"bill";
        
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:s1,s2,s3,nil];
        
        NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];//kvc
        
        [arr3 sortUsingDescriptors:[NSArray arrayWithObject:sort2]];
        
        for (Student *stu in arr3)
        {
            NSLog(@"stu.name = %@",stu.name);
        }

    Comparator:通过代码块对”对象或对象属性“排序

    Student *s1 = [[Student alloc]init];
        s1.name = @"obanma";
        s1.age = 38;
        Student *s2 = [[Student alloc]init];
        s2.name = @"jobs";
        s2.age = 17;
        Student *s3 = [[Student alloc]init];
        s3.name = @"bill";
        s3.age = 25;
        NSArray *array = [[NSArray alloc]initWithObjects:s1,s2,s3,nil];
        // 升序排列name和age。降序的话,调换ascend和descaend位置。
        
        NSArray *sortAge = [array sortedArrayUsingComparator:^(id obj1,id obj2)
                            {
                                if([obj1 age] < [obj2 age])
                                {
                                    return (NSComparisonResult)NSOrderedAscending;//-1
                                }
                                else if ([obj1 age] > [obj2 age])
                                {
                                    return (NSComparisonResult)NSOrderedDescending;
                                }
                                
                                return (NSComparisonResult)NSOrderedSame;
                            }];
    
        for (Student *stu in sortAge)
        {
            NSLog(@"stu.age = %d",stu.age);//38,17,25
        }
        
        NSArray *sortArr = [array sortedArrayUsingComparator:^(id obj1,id obj2)
                            {
                                NSLog(@"%d",[[obj1 name] compare: [obj2 name]]);
                                
                                if ([[obj1 name] compare: [obj2 name]] == NSOrderedDescending)//1
                                {
                                    return (NSComparisonResult)NSOrderedDescending;//1
                                }
                                else if([[obj1 name] compare: [obj2 name]] == NSOrderedAscending)
                                {
                                    return (NSComparisonResult)NSOrderedAscending;//-1
                                }
                                return (NSComparisonResult)NSOrderedSame;
                            }];
        
        for (Student *stu in sortArr)
        {
            NSLog(@"stu.name = %@",stu.name);
        }

    http://blog.csdn.net/xiaoxuan415315/article/details/9198729

    http://blog.csdn.net/shx_yi/article/details/8687368

  • 相关阅读:
    Python 实现AEC CBC 加密解密方式
    redis 发布订阅方法与缺陷
    python paramiko 传输下载文件
    Redis 配置文件
    Redis 命令
    window11 | 虚拟机vmWare安装windows11
    十万个为什么 | 文化001-为什么猜灯谜又叫做打灯谜
    ffmpeg | 常用命令使用
    ffmpeg | 常用命令使用
    Adobe系列 | Animate(01)-软件安装
  • 原文地址:https://www.cnblogs.com/huen/p/3528794.html
Copyright © 2011-2022 走看看