iOS 第四期考核题(字符串/字典/数组的使用)

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)删除“陈丰”并输出数据

 NSString *str=@"2015-09-01 08:19:10";
        NSRange range1=[str rangeOfString:@"08:19:10"];
        NSString *subStr=[str substringWithRange:range1];
        NSLog(@"第一题-------(1)");
        NSLog(@"%@",subStr);
        NSString *substr1=[str substringFromIndex:range1.location];
        NSLog(@"第一题-------(2)");
        NSLog(@"%@",substr1);
        
        
        NSArray *arr1=@[@{@"name":@"陈丰",@"age":@"32"},
                        @{@"name":@"孙明",@"age":@"24"},
                        @{@"name":@"王明",@"age":@"12"},
                        @{@"name":@"李西",@"age":@"50"}
                        ];
        
        
        NSDictionary *dic1=@{@"name":@"陈丰",@"age":@"32"};
        NSDictionary *dic2=@{@"name":@"孙明",@"age":@"24"};
        NSDictionary *dic3=@{@"name":@"王明",@"age":@"12"};
        NSDictionary *dic4=@{@"name":@"李西",@"age":@"50"};
        NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4,nil];
        NSSortDescriptor *sorts=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
        [arr sortUsingDescriptors:[NSArray arrayWithObjects:sorts, nil]];
        NSLog(@"第二题-------(1)");
        NSLog(@"排序后-------(升序)");
        for(NSDictionary *dic in arr){
            NSLog(@"%@ %@",dic[@"name"],dic[@"age"]);
        }
        NSLog(@"排序后-------(降序)");
        NSSortDescriptor *sorts1=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO];
        [arr sortUsingDescriptors:[NSArray arrayWithObjects:sorts1, nil]];
        for(NSDictionary *dic in arr){
            NSLog(@"%@ %@",dic[@"name"],dic[@"age"]);
        }
        NSLog(@"第二题-------(2)");
        for(int i=0;i<[arr count];i++){
            NSDictionary *dic=arr[i];
            if([dic[@"name"] isEqual:@"陈丰"]){
                [arr removeObject:dic];
            }
        }
        for(NSDictionary *dic in arr){
            NSLog(@"%@ %@",dic[@"name"],dic[@"age"]);
        }

 

posted @ 2016-01-08 17:09  海绵宝宝つ丢了派大星  阅读(158)  评论(0编辑  收藏  举报