1 //1.以下是身份证号,输出身份证人的性别,年龄 2 // NSString *[email protected]; 3 //身份证倒数#2位是偶数则为女生 4 NSString *idCard=@"210423198809040427"; 5 int sex1=[[idCard substringWithRange:NSMakeRange(16, 1)] intValue]; 6 if((sex1%2)==0){ 7 NSLog(@"性别:女"); 8 }else{ 9 NSLog(@"性别:男"); 10 } 11 12 int age1=[[idCard substringWithRange:NSMakeRange(6, 4)] intValue]; 13 NSDate *date = [NSDate date];//这个是NSDate类型的日期,所要获取的年月日都放在这里; 14 NSCalendar *cal = [NSCalendar currentCalendar]; 15 NSUInteger unitFlags = NSCalendarUnitYear;//这句是说你要获取日期的元素有哪些 16 NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面; 17 //然后就可以从d中获取具体的年月日了; 18 NSInteger year = [d year]; 19 NSUInteger age2=year-age1+1; 20 NSLog(@"%ld",age2); 21 //2.将字典的key从Z->A排序,按排序后的key的顺序,输出value,将value按字符串输出 22 NSDictionary *[email protected]{@"R":@"e", 23 @"T":@"e", 24 @"D":@"b", 25 @"S":@"u", 26 @"K":@"a", 27 @"A":@"s", 28 @"O":@" ", 29 @"N":@"p", 30 @"B":@"b", 31 @"J":@"u", 32 @"F":@" ", 33 @"U":@"t", 34 @"H":@"l", 35 @"E":@"j", 36 @"Z":@"s", 37 38 }; 39 //获取所有的key值放在数组内 40 NSArray *keys=[dict allKeys]; 41 //对数组内的key值进行排序(降序) 42 NSArray *sortkeys=[keys sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){ 43 return [obj2 compare: obj1]; 44 }]; 45 //根据key值输出value值 46 for(id key in sortkeys){ 47 id obj=[dict objectForKey:key]; 48 NSLog(@"%@",obj); 49 }
时间: 2024-10-10 16:36:47