Sweeper
Sweeper

Reputation: 272715

What is the max value of Int in different iOS versions?

I was building this app and it needs to convert a string to an Int. So I did this:

Int(someString)

However, sometimes someString is a very large number. Its value exceeds the max value of Int32. I know that the max value of Int can be different if the user uses different OSs.

I used iOS 9 and iOS 8 to test my app and it worked fine. This means that the max value of Int is 264 - 1, not the 32 bit one. But what about other iOS versions? If the max value of Int is smaller, my app would produce wrong outputs!

I just want to ask what are the respective max values of Int in different iOS versions? Which version'(s) max value is 32 bit?

Upvotes: 2

Views: 6873

Answers (2)

gnasher729
gnasher729

Reputation: 52592

Int is 32 bit on all 32 bit systems, and 64 bit on all 64 bit systems. Usually you will build a universal binary that runs on all processors, then it depends on the processor in the device. Or you build 32 bit only (for iOS that's not accepted on the App Store anymore), and it will always be 32 bit.

You can just use Int64 in Swift, which is always 64 bits. For counting things within your app, like how many elements in an array, you should always use Int. For things outside your code, use Int if +/- two billion is guaranteed to be enough, Int64 if +/- 8 billion billion is enough, and if that isn't enough you have a problem :-)

Plan ahead. For example I believe Twitter initially thought 32 bits was enough to number all tweets ever sent. It wasn't, which caused trouble for a while.

Upvotes: 3

Michael
Michael

Reputation: 9042

The size of an integer is dependent upon the processor architecture, not the operating system. You can use conditional compilation to determine what you're currently running on. for example:

    #if arch(arm64)
    // 64-bit
    #end

The documentation here (under Build Configurations) shows what you can test for. You can also use os() to determine the operating system.

Note that for some reason on my laptop, the Simulator runs iPad Retina as 32-bit, even though the actual device is 64-bit.

Upvotes: 2

Related Questions