#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
#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岁
NSSortDescriptor *p1=[NSSortDescriptor sortDescriptorWithKey:kAge ascending:1];
NSArray *p=[array sortedArrayUsingDescriptors:[NSArray arrayWithObject:p1]];
for (NSDictionary *dic in p) {
if([ [dic objectForKey:kGender] isEqual:@YES])
{
[dic setValue:@"男性" forKey:kGender];
NSLog(@"姓名:%@,性别:%@,年龄%@",dic[kName],dic[kGender],dic[kAge]);
}else{
[dic setValue:@"女性" forKey:kGender];
NSLog(@"姓名:%@,性别:%@,年龄%@",dic[kName],dic[kGender],dic[kAge]);
}
}
//第2题 请输入女性信息,输出样式为:范冰冰大美女,今年35岁
NSLog(@"----------------------------------");
for (NSDictionary *dic in array) {
if([ [dic objectForKey:kGender] isEqual:@"女性"]){
NSString *str2=@"大美女";
NSString *newstr1=[dic[kName] stringByAppendingString:str2];
[dic setValue:newstr1 forKey:kName];
}
NSLog(@"姓名:%@,性别:%@,年龄%@",dic[kName],dic[kGender],dic[kAge]);
}
//第3题 请将“马云”的年龄改为41岁
NSLog(@"----------------------------------");
for (NSDictionary *dic in array) {
if([ [dic objectForKey:kName] isEqual:@"马云"]){
[dic setValue:@"41" forKey:kAge];
}
NSLog(@"姓名:%@,性别:%@,年龄%@",dic[kName],dic[kGender],dic[kAge]);
}
}
return 0;
}