iOS 第六期考核题(字典的使用)
NSArray *array = @[CreateDict(@"王思聪", 27, YES),
CreateDict(@"范冰冰", 35, NO),
CreateDict(@"马云", 51, YES),
CreateDict(@"霍建华", 36, YES),
CreateDict(@"赵丽颖", 28, NO)];
//第1题 请按照年龄的升序排序输出,输出样式为:王思聪,男性,今年27岁
//第2题 请输入女性信息,输出样式为:范冰冰大美女,今年35岁
//第3题 请将“马云”的年龄改为41岁
1 NSArray *array = @[CreateDict(@"王思聪", 27, YES), 2 CreateDict(@"范冰冰", 35, NO), 3 CreateDict(@"马云", 51, YES), 4 CreateDict(@"霍建华", 36, YES), 5 CreateDict(@"赵丽颖", 28, NO)]; 6 //NSLog(@"%@",array);
//使用描述器对字典按关键字进行排序 7 NSSortDescriptor *ageSort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
//调用排序方法 返回对象类型是数组类型 8 NSArray *arr=[[NSArray alloc]initWithObjects:ageSort, nil]; 9 NSMutableArray *newArray=[array sortedArrayUsingDescriptors:arr]; 10 NSString *str=[NSString string]; 11 NSLog(@"第一题------------"); 12 for(NSDictionary *dic in newArray){ 13 if([dic[@"gender"] isEqual:@1]){ 14 str=@"男性"; 15 }else{ 16 str=@"女性"; 17 } 18 NSLog(@"%@,%@,今年%@岁",dic[@"name"],str,dic[@"age"]); 19 } 20 NSLog(@"第二题------------"); 21 for(NSDictionary *dic in array){ 22 if([dic[@"gender"] isEqual:@0]){ 23 NSLog(@"%@大美女,今年%@岁",dic[@"name"],dic[@"age"]); 24 } 25 } 26 NSLog(@"第三题------------"); 27 for(int i=0;i<[newArray count];i++){ 28 NSMutableDictionary *dic = newArray[i]; 29 if([dic[@"name"] isEqual:@"马云"]){ 30 NSLog(@"修改前"); 31 NSLog(@"%@,今年%@岁",dic[@"name"],dic[@"age"]); 32 NSLog(@"修改后"); 33 //NSDictionary *dict=@{@"name":dic[@"name"], 34 // @"age":@41, 35 //@"gender":dic[@"gender"]}; 36 //dic[kAge]=@41; 37 dic[@"age"]=@41; 38 //dic=[NSDictionary dictionaryWithDictionary:dict]; 39 NSLog(@"%@,今年%@岁",dic[@"name"],dic[@"age"]); 40 41 } 42 }