I have a phone number formatted as an easy to read phone number, i.e., (123) 123-4567
However, when I want to use that phone number in code, I need to parse that string into a number variable (such as an int
)
However, [string intValue];
doesn't work - I've used intValue
about a million times in previous projects, all with no problem, however here, every string I pass in, I get the same, random int
back out:
- (int)processPhoneNumber:(NSString *)phoneNumber {
NSMutableString *strippedString = [NSMutableString stringWithCapacity:10];
for (int i=0; i<[phoneNumber length]; i++) {
if (isdigit([phoneNumber characterAtIndex:i])) {
[strippedString appendFormat:@"%c",[phoneNumber characterAtIndex:i]];
}
}
NSLog(@"clean string-- %@", strippedString);
int phoneNumberInt = [strippedString intValue];
NSLog(@"**** %d\n &i", phoneNumberInt, phoneNumberInt);
return phoneNumberInt;
}
Then when I call this method:
NSLog(@"%i", [self processPhoneNumber:self.phoneNumberTextField.text]);
I get: 2147483647
. Every input I give this method returns: 2147483647