标签:
#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 arrayWithArray:arr];
[newarr addObject:@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}];
for(id str in newarr){
NSLog(@"%@",str);
}
//2.查找数组内"Steve Jobs"的数据并删除。
NSMutableArray *muarr=[NSMutableArray array];
for (int i=0; i<newarr.count; i++) {
NSDictionary *dic1=newarr[i];
if ( ![dic1[@"name"] isEqual:@"Steve Jobs"]) {
[muarr addObject:dic1];
}
}
for (NSDictionary *dic in muarr) {
NSLog(@"姓名:%@,年龄:%@",dic[@"name"],dic[@"age"]);
}
//3.按姓名首字母进行排序。
NSSortDescriptor *nameWithSort=[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSArray *elementarr=[NSArray arrayWithObject:nameWithSort];
NSArray *sortArray=[arr sortedArrayUsingDescriptors:elementarr];
for (NSDictionary *dic in muarr) {
NSLog(@"姓名:%@",dic[@"name"]);
}
NSLog(@"\n");
//4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
NSSortDescriptor *ageWithSort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:1];
NSSortDescriptor *sexWithSort=[[NSSortDescriptor alloc]initWithKey:@"sex" ascending:1];
NSArray *newgood=[muarr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:ageWithSort,sexWithSort, nil]];
for (NSDictionary *dic1 in newgood) {
NSLog(@"姓名:%@ %@",dic1[@"name"],dic1[@"sex"]);
}
//5.输出成绩大于或等于80分的学员信息。
for (NSDictionary *dict in newgood) {
int b=[dict[@"score"] intValue];
if(b>80){
NSLog(@"姓名:%@,年龄:%@,性别%@,分数:%@",dict[@"name"],dict[@"age"],dict[@"sex"],dict[@"score"]);
}
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/jidezhi/p/5121738.html