Tag Archives: 64bit

How to covert an NSUInteger / NSInteger into an int value

Before the addition of 64bit in iOS it was possible to take an NSUInteger (or NSInteger) and put it into an int variable, like so:

If you add 64bit support to your app you may notice a compiler warning when you do this:

[emember_protected]

Implicit conversion loses integer precision: ‘NSInteger’ (aka ‘long’) to ‘int’

Your code will still work, but what Xcode is saying here is that in 64bit an NSInteger is not the same as an int value because it’s classic C and still 32bit. Upon conversion half those bits are cut off.

This is not a problem if your value is anywhere between 0 and 4,294,967,295 (or between −2,147,483,648 and 2,147,483,647 respectively).

But a 64bit integer can hold values between 0 and 18,446,744,073,709,551,615 (or between −9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 respectively) – which even if we can’t imagine or read such a number, it appears considerably higher than the one above.

Those scary values aside, how do we tell Xcode that “it’s fine to drop several gazillion from my array with 12 items”?

We can use typecasting. Rather than “implying” that we’d like to convert the value, we explicitly tell Xcode to do so:

[/emember_protected]

Xcode understands that this is intentional and drops the warning.

How to check if your app is running in 64bit

There are two ways to determine this: at runtime (i.e. in your running code), and at compile time (i.e. before the code is compiled). Let’s take a look at both options.

Runtime Check

Determines the size of a pointer.

Compile Time Check

Asks the compiler which architecture it compiles for.

How to add 64bit support to your iOS apps

Apple’s new A7 processors support 64bit under iOS 7 – but only if your app is specifically compiled for 64bit. Ideally you want to compile once and use 64bit if the architecture supports it, and use 32bit for devices that have A4 – A6x processors and hence do not support 64bit.

Since Xcode 5.0.2 you can do this, and here’s how:

  • head over to your app’s target and choose Build Settings
  • under Architectures, choose “Standard architectures, including 64bit”
  • Xcode will offer to “Change the deployment target to iOS 5.1.1”
  • agree to it and you’re done

Now your app will choose the 64bit frameworks where available, and otherwise use 32bit.

Screen Shot 2013-11-16 at 12.29.24