#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#import <Foundation/Foundation.h>
#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数组内。
2.查找数组内"Steve Jobs"的数据并删除。
3.按姓名首字母进行排序。
4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
5.输出成绩大于或等于80分的学员信息。*/
NSDictionary *dic=@{@"name":@"Philip Schiller",
@"age":@"29",
@"sex":@"female",
@"score":@"70"};
NSMutableArray *arr3=[NSMutableArray arrayWithArray:arr];
[arr3 addObject:dic];
for(NSDictionary *dic1 in arr3)
{
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]);
}
NSLog(@"\n");
//2、查找数组内"Steve Jobs"的数据并删除。
NSMutableArray *arr4=[NSMutableArray array];
for(int i=0;i<arr3.count;i++)
{
NSMutableDictionary *dic2=arr3[i];
if(![dic2[@"name"] isEqualTo:@"Steve Jobs"])
{
[arr4 addObject:dic2];
}
}
for(NSDictionary *dic3 in arr4)
{
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic3[@"name"],dic3[@"age"],dic3[@"sex"],dic3[@"score"]);
}
NSLog(@"\n");
// 3.按姓名首字母进行排序。
NSSortDescriptor *sortWithName=[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSArray *eleSort=[NSArray arrayWithObjects:sortWithName,nil];
NSArray *sortArr=[arr4 sortedArrayUsingDescriptors:eleSort];
for(NSDictionary *dic4 in sortArr){
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic4[@"name"],dic4[@"age"],dic4[@"sex"],dic4[@"score"]);
}
NSLog(@"\n");
//4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
NSSortDescriptor *sortWithAge=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
NSSortDescriptor *sortWithSex=[[NSSortDescriptor alloc]initWithKey:@"sex" ascending:YES];
NSArray *eleSorts=[NSArray arrayWithObjects:sortWithAge,sortWithSex,nil];
NSArray *sortArrs=[arr4 sortedArrayUsingDescriptors:eleSorts];
for(NSDictionary *dic4 in sortArrs){
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic4[@"name"],dic4[@"age"],dic4[@"sex"],dic4[@"score"]);
}
NSLog(@"\n");
//5.输出成绩大于或等于80分的学员信息。
NSMutableArray *m=[NSMutableArray array];
for(int i=0;i<arr.count;i++)
{
NSDictionary *dic6=arr[i];
NSInteger n=[dic6[@"score"] intValue];
if(n>=80)
{
[m addObject:dic6];
}
}
for(NSDictionary *dic7 in m){
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic7[@"name"],dic7[@"age"],dic7[@"sex"],dic7[@"score"]);
}
}
return 0;
}