字符串、集合补充

本文介绍了Objective-C中数组与字符串间的转换方法,包括使用componentsSeparatedByString和componentsJoinedByString进行转换。同时,详细讲解了如何利用NSSortDescriptor进行简单及复杂对象的排序,并通过代码块实现自定义排序。
摘要由CSDN通过智能技术生成

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

转载于:https://www.cnblogs.com/huen/p/3528794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值