Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly Converting NSString to CGFloat, NSInteger, etc

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?

like image 617
CIFilter Avatar asked Dec 02 '09 19:12

CIFilter


1 Answers

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.

like image 99
Chuck Avatar answered Nov 10 '22 03:11

Chuck