#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
#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"的数据并删除。
//法2
for (int i=0;i<[mutarray count];i++) {
NSDictionary *dic=mutarray[i];
if([ [dic objectForKey:@"name"] isEqual:@"Steve Jobs"])
{
[mutarray removeObject:dic];
}
}
for (NSDictionary *dic1 in mutarray) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]);
}
3.按姓名首字母进行排序。
4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
5.输出成绩大于或等于80分的学员信息。*/
//1.添加数据姓名:Philip Schiller年龄:29性别:female分数:70到arr数组内。
NSMutableArray *mutarray=[[NSMutableArray alloc]initWithArray:arr];
[mutarray addObject:@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}];
for(id str in mutarray){
NSLog(@"%@",str);
}
//2.查找数组内"Steve Jobs"的数据并删除。
NSLog(@"查找数组Steve Job的数据并删除");
NSArray *arr0=[[NSArray alloc]initWithArray:mutarray];
for (id dic in arr0) {
if([ [dic objectForKey:@"name"] isEqual:@"Steve Jobs"])
{
[mutarray removeObject:dic];
}
}
for (NSDictionary *dic1 in mutarray) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dic1[@"name"],dic1[@"age"],dic1[@"sex"],dic1[@"score"]);
}
NSLog(@"\n");
//3.按姓名首字母进行排序。
NSLog(@"按姓名首字母进行排序");
NSSortDescriptor *p1=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:1];
NSArray *p=[mutarray sortedArrayUsingDescriptors:[NSArray arrayWithObject:p1]];
for (NSDictionary *dict in p) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
NSLog(@"\n");
//4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
NSLog(@"按年龄进行升序排序,如果年龄相同则按性别进行排序");
NSSortDescriptor *p3=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:1];
NSSortDescriptor *p4=[NSSortDescriptor sortDescriptorWithKey:@"sex" ascending:1];
NSArray *p5=[mutarray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:p3,p4, nil]];
for (NSDictionary *dict in p5) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
NSLog(@"\n");
//5.输出成绩大于或等于80分的学员信息。
for (NSDictionary *dict in mutarray) {
int b=[dict[@"score"] intValue];
if(b>80){
NSLog(@"姓名:%@,年龄:%@,性别%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
}
}
return 0;
}