Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Int Types in iPhone 5 and iPhone 6

Tags:

int

ios

swift

I am reading a static Stream of Ints into an Array of Ints:

func create_int_array_from_nsmutable(nsdata:NSMutableData)  -> [Int] {
    let pointer = UnsafePointer<Int>(nsdata.bytes)
    let count = nsdata.length / sizeof(Int);
    // Get buffer pointer and make an array out of it
    let buffer = UnsafeBufferPointer<Int>(start:pointer, count:count)
    let array = [Int](buffer)
    return array;
}

Now I realize, that in the iPhone 5 an Int has a size of 4 and on iPhone 6 or the iPad has a size of 8, which leads to totally different results.

Has someone a hint how to read this stream in both cases and get the same result?

Should I use the Int64 Type? Does Int64 exists exact for that reason, e.g. to solve compatibility problems?

like image 765
mcfly soft Avatar asked Feb 08 '23 22:02

mcfly soft


1 Answers

The statement that iPhones and iPads have a different size for the Int value type is wrong, 32-bit and 64-bit processors have this difference.

From the Swift docs:

  • On a 32-bit platform, Int is the same size as Int32.
  • On a 64-bit platform, Int is the same size as Int64.

Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.

If you need to keep your Integer size consistent across multiple architectures, use Int32.

like image 192
JAL Avatar answered Feb 10 '23 10:02

JAL