#import <Foundation/Foundation.h> static NSString * const kName = @"name"; static NSString * const kAge = @"age"; static NSString * const kGender = @"gender"; inline static NSMutableDictionary *CreateDict(NSString *name, NSInteger age, BOOL isMale){ NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{kName:name,kAge :@(age),kGender :@(isMale)}]; return dict; } NSString *maleFemale(id sex){ if([sex isEqual:@YES]){ return @"男性"; } else{ return @"女性"; } } int main(int argc, const char * argv[]) { @autoreleasepool { NSArray *array = @[CreateDict(@"王思聪", 27, YES), CreateDict(@"范冰冰", 35, NO), CreateDict(@"马云", 51, YES), CreateDict(@"霍建华", 36, YES), CreateDict(@"赵丽颖", 28, NO)]; //第1题 请按照年龄的升序排序输出,输出样式为:王思聪,男性,今年27岁 NSSortDescriptor *sortWithAge=[[NSSortDescriptor alloc]initWithKey:kAge ascending:YES]; NSArray *eleSort=[NSArray arrayWithObjects:sortWithAge,nil]; NSArray *sortArr=[array sortedArrayUsingDescriptors:eleSort]; for(NSDictionary *dic in sortArr){ NSLog(@"%@,%@,今年%@岁",dic[@"name"],maleFemale(dic[kGender]),dic[@"age"]); } //第2题 请输入女性信息,输出样式为:范冰冰大美女,今年35岁 for(NSDictionary *dic1 in sortArr){ if([dic1[kGender] isEqual:@NO]){ NSLog(@"%@大美女,今年%@岁",dic1[kName],dic1[kAge]); } } //第3题 请将“马云”的年龄改为41岁 NSMutableArray *mutArr=[NSMutableArray arrayWithArray:array]; for(NSDictionary *dic in mutArr) { if([dic[kName] isEqualToString:@"马云"]){ [dic setValue:@"41" forKey:kAge]; } } for(NSDictionary *dic2 in sortArr){ NSLog(@"%@,%@,%@",dic2[kName],maleFemale(dic2[kGender]),dic2[kAge]); } } return 0; }