考核题 4
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //1.对字符串“2015-09-01 08:19:10”操作,输出"08:19:10"。要求使用两种截取方式。 /*2.数组中存取以下数据: @[@{@"name":@"陈丰",@"age":@"32"}, @{@"name":@"孙明",@"age":@"24"}, @{@"name":@"王明",@"age":@"12"}, @{@"name":@"李西",@"age":@"50"} ] 1)按年龄进行排序(降序),输出数据 2)删除“陈丰”并输出数据 */ NSLog(@"第一题:"); NSString *str=@"2015-09-01 08:19:10"; NSString *str1=[str substringFromIndex:10]; NSLog(@"%@",str1); NSRange range; range.length=9; range.location=10; NSString *str2=[str substringWithRange:range]; NSLog(@"%@",str2); NSLog(@"第二题:"); NSArray *arr=@[@{@"name":@"陈丰",@"age":@"32"}, @{@"name":@"孙明",@"age":@"24"}, @{@"name":@"王明",@"age":@"12"}, @{@"name":@"李西",@"age":@"50"} ]; NSSortDescriptor *ageSort2=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES]; NSArray *newarr2=[NSArray arrayWithObject:ageSort2]; NSArray *sort2=[arr sortedArrayUsingDescriptors:newarr2]; NSLog(@"升序输出:"); for (NSDictionary *dic in sort2) { NSLog(@"姓名:%@,年龄:%@",dic[@"name"],dic[@"age"]); } NSSortDescriptor *ageSort1=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO]; NSArray *newarr1=[NSArray arrayWithObject:ageSort1]; NSArray *sort1=[arr sortedArrayUsingDescriptors:newarr1]; NSLog(@"降序输出:"); for (NSDictionary *dic in sort1) { NSLog(@"姓名:%@,年龄:%@",dic[@"name"],dic[@"age"]); } NSLog(@"删除后:"); //NSMutableArray *newarr=[NSMutableArray alloc]; for (int i=0;i<arr.count;i++) { NSDictionary *dic=arr[i]; if (![[dic objectForKey:@"name"]isEqual:@"陈丰"]) { //[newarr addObject:dic]; NSLog(@"姓名:%@,年龄:%@",dic[@"name"],dic[@"age"]); } } } return 0; }