The compiler that comes with latest Xcode now have additional warnings, some of them can help you deal with the 64-bit migration.
For example, in this snippet of code:
NSInteger iVal = 1;
NSLog(@“%ld”, iVal);
You will see this warning:
warning: values of type ‘NSInteger’ should not be used as format arguments; add an explicit cast to ‘long’ instead
Background
OS X/iOS uses several data types such as NSInteger, NSUInteger,CGFloat to provide a consistent means of representing values in 32- and 64-bit environments.
In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively.
In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively.
Something like that in the Foundation header files:
#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
To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown here:
| Type |
Format specifier |
Considerations |
| NSInteger |
%ld or %lx |
Cast the value to long |
| NSInteger |
%ld or %lx |
Cast the value to unsigned long |
| CGFloat |
%f or %g |
Both works for floats and doubles |
Why this warning only happens in release build?
It is because in the auto-created build configuration, the flag of Build Active Architecture Only is defaulted to YES.
The target (simulator or plugged-in device) detected from Clang compiler is 64-bit.
The casting warning will not be raised as the NSInteger is mapped to ‘long’ already.
Alternative
Another way to avoid this warning is to box the NSInteger/NSUInteger variable in NSNumber, using the literal syntax.
NSInteger iVal = 1;
NSLog(@”%@”, @(iVal));
However, it may drag down the performance a bit if the log statement is called a lot.