[示例]NSDictionary编程题-字典的排序应用(iOS4班)
代码:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //1.对字符串“2015-09-01 08:19:10”操作,输出"08:19:10"。要求使用两种截取方式。 NSString *time=@"2015-09-01 08:19:10"; NSString *str1=[time substringFromIndex:11 ]; NSRange rang={11,8}; NSString *str2=[time substringWithRange:rang]; NSLog(@"index:%@,range:%@",str1,str2); /*2.数组中存取以下数据: @[@{@"name":@"陈丰",@"age":@"32"}, @{@"name":@"孙明",@"age":@"24"}, @{@"name":@"王明",@"age":@"12"}, @{@"name":@"李西",@"age":@"50"} ] 1)按年龄进行排序(降序),输出数据 2)删除“陈丰”并输出数据 */ NSDictionary *dic1=@{@"name":@"陈丰",@"age":@"32"}; NSDictionary *dic2=@{@"name":@"孙明",@"age":@"24"}; NSDictionary *dic3=@{@"name":@"王明",@"age":@"12"}; NSDictionary *dic4=@{@"name":@"李西",@"age":@"50"}; NSArray *arr=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,nil]; NSSortDescriptor *disc=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO]; NSArray *eleSort=[NSArray arrayWithObjects:disc,nil]; NSArray *arrSort=[arr sortedArrayUsingDescriptors:eleSort]; for(NSDictionary *dic in arrSort){ NSLog(@"name:%@,age:%@",dic[@"name"],dic[@"age"]); } NSMutableArray *mutArr=[NSMutableArray arrayWithArray:arrSort]; for(int i=0;i<mutArr.count;i++){ if([mutArr[i][@"name"] isEqual:@"陈丰"]){ [mutArr removeObject:mutArr[i]]; } } for(NSDictionary *dic2 in mutArr){ NSLog(@"name:%@,age:%@",dic2[@"name"],dic2[@"age"]); } } return 0; }