标签:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSArray *arr=@[@{@"name":@"Tim Cook",@"age":@"24",@"sex":@"female",@"score":@"89"},@{@"name":@"Jony Ive",@"age":@"26",@"sex":@"female",@"score":@"76"},@{@"name":@"Steve Jobs",@"age":@"24",@"sex":@"male",@"score":@"67"},@{@"name":@"Robert Brunne",@"age":@"28",@"sex":@"male",@"score":@"88"}]; /* 1.添加数据姓名:Philip Schiller年龄:29性别:female分数:70到arr数组内。 */ NSMutableArray *newarr= [[NSMutableArray alloc]initWithArray:arr]; NSDictionary *arr1=@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}; [newarr addObject:arr1]; /* 2.查找数组内"Steve Jobs"的数据并删除。*/ for (int i=0; i<[newarr count]; i++) { NSDictionary *dic=newarr[i]; if ([dic[@"name"]isEqual:@"Steve Jobs"]) { [newarr removeObject: dic]; } } /*3.按姓名首字母进行排序。*/ NSSortDescriptor *des=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:1]; NSArray *arr2 =[newarr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:des, nil]]; for (NSDictionary *dic in arr2) { NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } /*4.按年龄进行升序排序,如果年龄相同则按性别进行排序。*/ NSSortDescriptor *des1=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:1]; NSSortDescriptor *des4=[NSSortDescriptor sortDescriptorWithKey:@"sex" ascending:1]; NSArray *arr3 =[newarr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:des1,des4, nil]]; for (NSDictionary *dic1 in arr3) { NSLog(@"%@ %@ %@ %@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]); } /*5.输出成绩大于或等于80分的学员信息。*/ for (NSDictionary *dic3 in arr2) { int num=[dic3[@"score"] intValue]; if (num>=80) { NSLog(@"%@ %@ %@ %@",dic3[@"name"],dic3[@"age"],dic3[@"sex"],dic3[@"score"]); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/jun1994/p/5121640.html