0

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

jscs
  • 63,694
  • 13
  • 151
  • 195
Andrew
  • 3,874
  • 5
  • 39
  • 67
  • 3
    I suggest that you're on entirely the wrong track. A phone "number" isn't really a number. It doesn't represent a _quantity_. You would never perform arithmetic on one. It's actually a string of digits, and you should handle it that way. See [What's the right way to represent phone numbers?](http://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers) – jscs Oct 21 '12 at 19:28
  • I'm 100% with @Josh Caswell here. In your int representation, you can't even tell if you need to call some "0" at the beginning! – Eiko Oct 21 '12 at 19:30
  • Did you step through your code using the Xcode debugging tools to find out where the problem arises? Knowing which part of your code doesn't work properly would help a lot. – Tim Vermeulen Oct 21 '12 at 19:31
  • That makes sense and I agree. I made it an int so I could easily call it, but your explanation makes more sense. If you make that into an answer, I'll accept it – Andrew Oct 21 '12 at 19:31

2 Answers2

5

As CRD has already explained, you're trying to convert that string to a type that's too small to hold the value, and you're getting back the maximum possible value for that type.

Your real problem is deeper, however. A phone "number" isn't really a number. It doesn't represent a quantity. You would never perform arithmetic on one. It's actually a string of digits, and you should handle it that way. Don't convert it; just operate on the string.

See also What's the right way to represent phone numbers?

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
4

2147483647 is INT_MAX which is returned on overflow. How large is your phone number, will it fit into an int? Maybe you should use longLongValue?

CRD
  • 52,522
  • 5
  • 70
  • 86
  • my phone numbers are 10 digits - as it that overflow value. but say the area code is 803, would that mean a number thats 10 digits long, starting with 803 would overflow? – Andrew Oct 21 '12 at 19:28
  • @Andrew - yes it will overflow. Your `int` is 32-bit signed. Use `long long` (or `UInt64` etc.) to get a 64-bit unsigned value. – CRD Oct 21 '12 at 19:31
  • Thanks. I've never really understood the difference between int, long, short, signed, unsigned, etc. – Andrew Oct 21 '12 at 19:32
  • @Andrew - On Mac OS X `int` is always 32 bits, `long long` is always 64 bits, and `long` depends on how you compile. And different systems use different models for 64-bit - see . – CRD Oct 21 '12 at 19:38