objective-cswiftintnsintegerint32

Objective-C int is bridged as Int32 to Swift


If I'm correct, Objective-C's int type length depends on the platform word length (i.e. it's 64 bit when running in a 64-bit environment and 32-bit when running in a 32-bit environment).

However, when bridging an Objective-C interface to Swift, this type is bridged as Int32 which explicitly defines the size of 32 bits.

Is this an error, or int in Objective-C always occupies 32 bit? If it's not an error, why it gets bridged as Int32?

Example of the Objective-C interface

-(int)numberOfItems;

Which gets bridged into:

func numberOfItems -> Int32

When I change the method signature to use NSInteger, it get's bridged correctly:

-(NSInteger)numberOfItems;
func numberOfItems -> Int

Is there any danger if I change int in Objective-C to NSInteger, or these types behave in exactly the same manner in Objective-C?


Solution

  • Objective C's int (or to be correct, C's int) should be at least 16 bits in size, so storing it in 32 bits is not invalid. Probably in Apple's implementation it is always 32 bit for int.

    If I'm correct, Objective-C's int type length depends on the platform word length (i.e. it's 64 bit when running in a 64-bit environment and 32-bit when running in a 32-bit environment).

    That would be NSInteger, not int. You may be confusing them because in Swift's Int is equivalent type for Objective C's NSInteger.

    You can even see it in the docs:

    typealias NSInteger = Int