#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岁
//char a,b;
NSSortDescriptor *sortArr=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:1];
NSArray *arr=[array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortArr]];
for(id sort in arr){
NSDictionary * xin=sort;
int sex=[xin[kGender] intValue];
if (sex==1) {
NSLog(@"%@,男性,%@",xin[kName],xin[kAge]);
}else{
NSLog(@"%@,女性,%@",xin[kName],xin[kAge]);
}
}
NSLog(@"????????????????????????????????????????????????????????");
//第2题 请输入女性信息,输出样式为:范冰冰大美女,今年35岁
for(id Male in arr){
NSDictionary *xin1=Male;
int sex=[xin1[kGender] intValue];
if (sex==0) {
NSLog(@"%@大美女,今年%@岁",xin1[kName],xin1[kAge]);
}
}
NSLog(@"????????????????????????????????????????????????????????");
//第3题 请将“马云”的年龄改为41岁
for(id age1 in arr){
NSDictionary *xin2=age1;
if ([[xin2 objectForKey:kName ]isEqual:@"马云"] ) {
[xin2 setValue:@"41" forKey:kAge];
}
}
for(id sort in arr){
NSDictionary * xin=sort;
int sex=[xin[kGender] intValue];
if (sex==1) {
NSLog(@"%@,男性,%@",xin[kName],xin[kAge]);
}else{
NSLog(@"%@,女性,%@",xin[kName],xin[kAge]);
}
}
return 0;
}
}