문제

I am creating a bitmask for the iOS using the data type int32_t. This is then set to a variable that accepts an NSInteger. This throws no compile time errors as expected, but I was wondering - is there was some way that this could cause run-time errors in the future?

도움이 되었습니까?

해결책

In general, it will work. NSInteger will always be at least 32 bits long. If it is 64 bits, your number will be sign extended to match (you might need to cast it). It will not cause run-time errors.


Detailed info about NSInteger

NSInteger is defined using the following code (from NSObjCRuntime.h):

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

This means that NSInteger is equivalent to long when compiling for 64 bit, an embedded OS, iPhone, Windows, or when building 32 bit like 64 bit. Otherwise, it is equivalent to int. Since int and int32_t are equivalent, you will be OK in 32 bit applications on OS X. For the other situations, it depends on the size of long.

In 64 bit OS X applications, long is a 64 bit number. On the iPhone, long is a 32 bit number. I do not know about Windows, and I am not sure whether it uses the 32 bit or 64 bit long when building 32 bit like 64 bit.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top