字典练习

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
NSDictionary *dic1=@{@"name":@"小阳",
                     @"class":@"IOS8",
                     @"age":@"24"
                     };
NSDictionary *dic2=@{@"name":@"小明",
                     @"class":@"IOS6",
                     @"age":@"67"
                     };
NSDictionary *dic3=@{@"name":@"小丽",
                    @"class":@"IOS8",
                    @"age":@"12"};
NSDictionary *dic4=@{@"name":@"小杨",
                    @"class":@"IOS8",
                     @"age":@"33"};
     
//1.将以上四个字典存入数组中,输出数据以班级:XXX,姓名:XXX,年龄:XX格式。
        NSArray *arr=[[NSArray alloc]initWithObjects:dic1,dic2,dic3,dic4, nil];
        for (NSDictionary *dic in arr) {
            NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
        }

//2.删除小明此条数据,列出剩余数据,输出格式同上。
        NSMutableArray *muarr=[NSMutableArray array];
        for (int i=0; i<arr.count; i++) {
            NSDictionary *dic=arr[i];
            if (![[dic objectForKey:@"name"]isEqual:@"小阳"]) {
                [muarr addObject:dic];
            }
        }
        NSLog(@"删除后");
        for (NSDictionary *dic in arr) {
            NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
        }
        
        
//3.按照班级进行排序,如果班级相同则按照年龄排序输出数据,格式同上。描述器
        //NSSortDescriptor 指定用于对象数组排序的对象属性
        NSSortDescriptor *classWithSort=[[NSSortDescriptor alloc]initWithKey:@"class" ascending:YES];
        NSSortDescriptor *ageWithSort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO];
        
        NSArray *sortArray=[arr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:classWithSort,ageWithSort, nil]];//先按照 班级 后年龄 排序
        NSLog(@"排序后");
        for (NSDictionary *dic in sortArray) {
            NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
        }
        
                     
    }
    return 0;
}

 

posted @ 2016-01-11 13:06  纽伦堡幅度  阅读(124)  评论(0编辑  收藏  举报