iOS 第七期考核题(字符串以及字典的使用 数组内容转换成字符串)
//1.以下是身份证号,输出身份证人的性别,年龄 // NSString *idCard=@210423198809040427; //身份证倒数#2位是偶数则为女生 NSString *idCard=@"210423198809040427"; int sex1=[[idCard substringWithRange:NSMakeRange(16, 1)] intValue]; if((sex1%2)==0){ NSLog(@"性别:女"); }else{ NSLog(@"性别:男"); } int age1=[[idCard substringWithRange:NSMakeRange(6, 4)] intValue]; NSDate *date = [NSDate date];//这个是NSDate类型的日期,所要获取的年月日都放在这里; NSCalendar *cal = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSCalendarUnitYear;//这句是说你要获取日期的元素有哪些 NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面; //然后就可以从d中获取具体的年月日了; NSInteger year = [d year]; NSUInteger age2=year-age1+1; NSLog(@"年龄 %ld",age2); //2.将字典的key从Z->A排序,按排序后的key的顺序,输出value,将value按字符串输出 NSDictionary *dict=@{@"R":@"e", @"T":@"e", @"D":@"b", @"S":@"u", @"K":@"a", @"A":@"s", @"O":@" ", @"N":@"p", @"B":@"b", @"J":@"u", @"F":@" ", @"U":@"t", @"H":@"l", @"E":@"j", @"Z":@"s", }; //获取所有的key值放在数组内 NSArray *keys=[dict allKeys]; //对数组内的key值进行排序(降序) NSArray *sortkeys=[keys sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){ return [obj2 compare: obj1]; }]; //根据key值输出value值 NSMutableArray *mutabarr=[NSMutableArray array]; for(id key in sortkeys){ id obj=[dict objectForKey:key]; //NSLog(@"%@",obj); [mutabarr addObject:obj]; }
//以字符串的形式输出value值 NSString *str=[mutabarr componentsJoinedByString:@""]; NSLog(@"%@",str); }