字符串、集合补充
来源:互联网 发布:linux root鉴定错误 编辑:程序博客网 时间:2024/05/18 00:13
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
0 0
- 字符串、集合补充
- OC-字符串集合补充
- 集合补充
- 集合补充
- java 集合补充
- 关于集合的补充
- java集合(待补充)
- 集合(补充1)
- Java基础 集合补充
- 列表浅谈,字符串补充
- 字符串补充-字符串的\0
- 经典算法集合,不断补充
- 黑马程序员-集合(补充总结)
- 集合的补充(Map)
- Java集合类:list补充
- 补充 - C++字符串完全指引
- 补充 - C++字符串完全指引
- Python补充05 字符串格式化
- 设计原则之最少知道原则
- 设计6大原则
- OC字符串
- 集合
- OC中实现可变参数
- 字符串、集合补充
- kvc、kvo、通知
- 动态类型
- 类目、延展、协议
- 归档
- NSDate、NSCalendar、NSDateFormatter
- NSFileManager、NSFileHandle
- 浅拷贝、深拷贝
- mysql数据库管理常用命令