码迷,mamicode.com
首页 > 移动开发 > 详细

[示例]NSDictionary编程题-字典的排序应用(iOS4班)

时间:2016-01-09 10:54:51      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

代码:

#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;
}

 

[示例]NSDictionary编程题-字典的排序应用(iOS4班)

标签:

原文地址:http://www.cnblogs.com/wangyang1987/p/5115473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!