4

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?

JAL
  • 41,701
  • 23
  • 172
  • 300
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • It is preferred if you can post separate questions instead of combining your questions into one. That way, it helps the people answering your question and also others hunting for at least one of your questions. Thanks! – Tobi Nary Jan 26 '16 at 14:25
  • 1
    iPhone 5 is 32 bit, while iPhone 6 is 64 bit. Hence, Int on iPhone 5 is same thing as Int32, and on iPhone 6 it's Int64. See this: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID319 – 0x416e746f6e Jan 26 '16 at 14:26

4 Answers4

8

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.

JAL
  • 41,701
  • 23
  • 172
  • 300
3

As the documentation gives away (side note, it just happens that different iPhone models have different-bit platforms):

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

Depending on the integer size you used to fill the stream, use the explicit version of that to read it correctly.

If you did not yet care about the sizes and their limits, consider using Int32 on both sides for additional space efficiency.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
  • Thanks a lot for explanation. Now its clear to me, that I should stream the stuff with Int32 for crosscompatibility. Thanks a lot. I upvote yours too. – mcfly soft Jan 26 '16 at 14:34
-1

You can for example do this test

let bit64 = false
if CGFLOAT_IS_DOUBLE == 1 {
  bit64 = true
}
Sten
  • 3,624
  • 1
  • 27
  • 26
  • Your code doesn't compile since `bit64` is a `let` constant and you can't mutate it (I didn't downvote). – JAL Jan 26 '16 at 20:26
-2

A possible solution (not the most performatic) before you create and array of int in from the NSData you can verify what is the device your app is running. See this answer and also this one to determine witch device you are running and then change the method call according to the device. Hope it has helped.

QUICK REFERENCE:

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone5,1"])
    // it's an iPhone 5
Community
  • 1
  • 1
  • 2
    That is no good solution. It would be preferable to use `sizeof()` to just know how big an Int is. – Tobi Nary Jan 26 '16 at 14:35
  • 1
    Indeed, presumably if you built an ARMv7 slice only, you'd run in 32-bit mode on a 64-bit device? Though I don't think you can still submit binaries without an ARM64 or Bitcode (which turns into ARM64) slice, so the observation is arguably academic. – Tommy Jan 26 '16 at 14:37
  • In the question he IS using the `sizeof()` function, I presume that either he is using it wrong or to is not working for him in this case. Also provided an alternate solution, as I mentioned "not the most performatic". – Felipe Cesar Assis Jan 26 '16 at 14:40
  • 2
    This is a horrible solution. Fixed with integer types exist for a reason. Additionally, this question is tagged [tag:swift] and not [tag:objective-c]. Why would you post an answer in Objective-C? – JAL Jan 26 '16 at 14:42