#import <Foundation/Foundation.h>
#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
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;
}
NSMutableDictionary *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岁
NSSortDescriptor *ageWithSort=[[NSSortDescriptor alloc]initWithKey:kAge ascending:YES];
NSArray *elementarr=[NSArray arrayWithObjects:ageWithSort, nil];
NSArray *sortArray=[array sortedArrayUsingDescriptors:elementarr];
for (NSDictionary *dic in sortArray) {
if([dic[kGender] isEqual:@"YES"]){
NSLog(@"%@,男性,今年%@岁",dic[kName],dic[kAge]);
}
else {
NSLog(@"%@,女性,今年%@岁",dic[kName],dic[kAge]);
}
}
NSLog(@"☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️");
//第2题 请输出女性信息,输出样式为:范冰冰大美女,今年35岁
for (NSDictionary *dic in sortArray) {
int num1=[dic [kGender]intValue];
if(num1==0){
NSLog(@"%@大美女,今年%@岁",dic[kName],dic[kAge]);
}
}
NSLog(@"☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️");
//第3题 请将“马云”的年龄改为41岁
for (NSDictionary *newdic in sortArray) {
if([[newdic objectForKey:kName] isEqual:@"马云"]) {
[newdic setValue:@"41" forKey:kAge];
}
}
for (NSDictionary *dic in sortArray) {
int num=[dic [kGender]intValue];
if(num==1){
NSLog(@"%@,男性,今年%@岁",dic[kName],dic[kAge]);
}
else if(num==0){
NSLog(@"%@,女性,今年%@岁",dic[kName],dic[kAge]);
}
}
}
return 0;
}