#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; } 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岁 //NSLog(@"%@",array); NSArray* array1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { if(obj1[kAge]==obj2[kAge]) { return NSOrderedSame; } else if (obj1[kAge]>obj2[kAge]) { return NSOrderedDescending; } else { return NSOrderedAscending; } }]; for(id obj in array1) { NSDictionary* dic = obj; NSInteger a = [obj[kGender] integerValue]; if(a==YES) { NSLog(@"%@ 男性 %@",dic[kName],dic[kAge]); } else { NSLog(@"%@ 女性 %@",dic[kName],dic[kAge]); } } //第2题 请输入女性信息,输出样式为:范冰冰大美女,今年35岁 NSLog(@"-----------------------------------"); for(id obj in array1) { NSDictionary* dic = obj; NSInteger a = [obj[kGender] integerValue]; if(a==NO) { NSLog(@"%@大美女 今年%@",dic[kName],dic[kAge]); } } NSLog(@"-----------------------------------"); //第3题 请将“马云”的年龄改为41岁 NSMutableArray* array2 = [NSMutableArray array]; for(int i = 0;i<array1.count;i++) { NSMutableDictionary* mdic = array1[i]; if([mdic[kName]isEqualTo:@"马云"]) { //字典替换 [mdic setValue:@41 forKey:kAge]; [array2 addObject:mdic]; } //[array2 addObject:mdic]; } for(id obj in array2) { NSDictionary* dic = obj; NSInteger a = [obj[kGender] integerValue]; if(a==YES) { NSLog(@"%@ 男性 %@",dic[kName],dic[kAge]); } else { NSLog(@"%@ 女性 %@",dic[kName],dic[kAge]); } } } return 0; }