SoFunction
Updated on 2025-04-13

IOS development implementation code for extracting gender from ID number

1. Code.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *sex=[self sexStrFromIdentityCard:@"139876456767892345"];
NSLog(@"--sex--%@",sex);
}
//Return to gender by ID number-(NSString *)sexStrFromIdentityCard:(NSString *)numberStr{
NSString *result = nil;
BOOL isAllNumber = YES;
if([numberStr length]<17)
return result;
//**The 17th intercepted is the gender identifierNSString *fontNumer = [numberStr substringWithRange:NSMakeRange(16, 1)];
//**Detection whether it is a number;const char *str = [fontNumer UTF8String];
const char *p = str;
while (*p!='\0') {
if(!(*p>='0'&&*p<='9'))
isAllNumber = NO;
p++;
}
if(!isAllNumber)
return result;
int sexNumber = [fontNumer integerValue];
if(sexNumber%2==1)
result = @"male";
///result = @"M";
else if (sexNumber%2==0)
result = @"female";
//result = @"F";
return result;
}

2. Output.

2015-10-22 10:33:38.518 Return to gender by ID number [2356:74542] --sex--Female

The above is the IOS development code that extracts gender from ID number introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!