views:

1503

answers:

1

I have a question about safely or properly converting an NSString into types such as CGFloat and NSInteger, considering that these are simply wrapper types that Apple created to use different primitives depending on certain factors (e.g., NSInteger is typedef'd to int on 32-bit architectures and long on 64-bit architectures).

For example, I know I can use something like [ someString intValue ] or [ someString longValue ], but how do I instead say that I want someString to become an NSInteger, allowing the precision of the underlying primitive to be chosen by the compiler? Saying something like NSInteger integer = [ someString intValue ] would result in data loss if someString was created from an NSInteger on a 64-bit system, right? What's the best way to deal with this situation?

+3  A: 

Use integerValue instead. And getting an int will only cause a problem if the integer represented by the string is greater than the 32-bit INT_MAX, but it's still better to use the more compatible method.

Chuck
Ah, thank you. I didn't even notice `integerValue`.
LucasTizma
That's why you should read the release notes every time a major OS version comes out. There are always a few handy additions to classes we've known and loved for many years.
NSResponder
What about for converting to CGFloat?
Casebash