Objective C - Why 64bit means different variable types

nsinteger, objective-c

Solution

Essentially it boils down to this: If you use CGFloat/NSInteger/etc, Apple can make backwards-incompatible changes and you can mostly update your app by just recompiling your code. You really don't want to be going through your app, checking every use of `int` and `double`.

What backwards-incompatible changes? Plenty!

- M68K to PowerPC

- 64-bit PowerPC

- PowerPC to x86

- x86-64 (I'm not sure if this came before or after iOS.)

- iOS

`CGFloat` means "the floating-point type that CoreGraphics" uses: `double` on OS X and `float` on iOS. If you use `CGFloat`, your code will work on both platforms without unnecessarily losing performance (on iOS) or precision (on OS X).

`NSInteger` and `NSUInteger` are less clear-cut, but they're used approximately where you might use `ssize_t` or `size_t` in standard C. `int` or `unsigned int` simply isn't big enough on 64-bit OS X, where you might have a list with more than ~2 billion items. (The unsignedness doesn't increase it to 4 billion due to the way `NSNotFound` is defined.)

Problem

I have heard that people use types such as NSInteger or CGFloat rather than int or float is because of something to do with 64bit systems. I still don't understand why that is necessary, even though I do it throughout my own code. Basically, why would I need a larger integer for a 64bit system? People also say that it is not necessary in iOS at the moment, although it may be necessary in the future with 64bit iphones and such.

Original source