#import <Foundation/Foundation.h>
#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
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 *dic=@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"};
[newarr addObject:dic];
for (NSDictionary *dict in newarr) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
//2.查找数组内"Steve Jobs"的数据并删除。
NSLog(@" ");
for (int i=0; i<[newarr count]; i++) {
NSDictionary *dic1=newarr[i];
if([dic1[@"name"]isEqual:@"Steve Jobs"]){
[newarr removeObject:dic1];
}
}
for (NSDictionary *dict8 in newarr) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict8[@"name"],dict8[@"age"],dict8[@"sex"],dict8[@"score"]);
}
//3.按姓名首字母进行排序。
NSLog(@" ");
NSSortDescriptor *ds1=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:1];
NSArray *ds=[newarr sortedArrayUsingDescriptors:[NSArray arrayWithObject:ds1]];
for (NSDictionary *dict in ds) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
// 4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
NSLog(@" ");
NSSortDescriptor *ds3=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:1];
NSSortDescriptor *ds4=[NSSortDescriptor sortDescriptorWithKey:@"sex" ascending:1];
NSArray *arr5=[newarr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:ds3,ds4, nil]];
for (NSDictionary *dict in arr5) {
NSLog(@"姓名:%@,年龄:%@,性别:%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
// 5.输出成绩大于或等于80分的学员信息。*/
NSLog(@" ");
for (NSDictionary *dict in newarr) {
int a=[dict[@"score"] intValue];
if(a>80){
NSLog(@"姓名:%@,年龄:%@,性别%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
}
}
return 0;
}