//
// main.m
// iOS6 .m
//
// Created by FuHeXiang on 16/1/9.
// Copyright (c) 2016年 FuHeXiang. All rights reserved.
//
#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*sex(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)];
//描述器排序 NSSortDescriptor
NSSortDescriptor *sortwithage=[[NSSortDescriptor alloc]initWithKey:kAge ascending:YES];
//
NSArray*elesort=[NSArray arrayWithObjects:sortwithage, nil];
NSArray*sortarr=[array sortedArrayUsingDescriptors:elesort];//使用描述
//第一题
for (id str in sortarr) {
NSLog(@"姓名为:%@,性别为:%@,年龄为:%@",str[@"name"],sex(str[kGender]),str[@"age"]);
}
//第二题
for (id str1 in sortarr) {
if ([str1[kGender] isEqual:@NO]) {
NSLog(@"%@是大美女,今年%@岁",str1[@"name"],str1[@"age"]);
}
}
//第三题 将马云的岁数更改为41
NSMutableArray*newarr=[NSMutableArray arrayWithArray:array];
for (id str2 in newarr) {
if ([str2[kName] isEqualToString:@"马云"]) {
[str2 setValue:@"41" forKey:kAge];
NSLog(@"姓名为:%@,性别为:%@,年龄为:%@",str2[@"name"],sex(str2[kGender]),str2[@"age"]);
}
}
//第四题、 将范冰冰的姓名更改为 老师
NSMutableArray*newname=[NSMutableArray arrayWithArray:array];
for (id str3 in newname) {
if ([str3[kName] isEqualToString:@"范冰冰"]) {
[str3 setValue:@"老师" forKey:kName];
NSLog(@"美女的姓名为%@",str3[@"name"]);
}
}
//第五题 输出1所有男性 女性不要
for ( id str4 in sortarr) {
if ([str4[kGender] isEqual:@YES]) {
NSLog(@"%@是帅哥,今年%@岁",str4[@"name"],str4[@"age"]);
}
}
//第六题 将范冰冰的年龄更改为100
NSMutableArray*newarr1=[NSMutableArray arrayWithArray:array];
for (id str5 in newarr1) {
if ([str5[kName] isEqualToString:@"王思聪"]) {
[str5 setValue:@"100" forKey:kAge];
NSLog(@"姓名为%@的年龄为%@",str5[@"name"],str5[@"age"]);
}
}
}
return 0;
}