Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%d formatting for both 32-bit and 64-bit

NSInteger precedence = [self operatorPrecedence];
[d appendFormat:@"precedence:%d, ", precedence];

gives:

Warning: Format specifies type 'int' but the argument has type 'NSInteger' (aka 'long')

and Xcode suggests to change %d to %ld.

However, it only works for either 32-bit or 64-bit target, as NSInteger is:

 #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

What's the best way to kill the warning, for both 32-bit and 64-bit targets?

like image 681
ohho Avatar asked Jan 23 '13 04:01

ohho


1 Answers

Follow the instructions in Apple's 64-Bit Transition Guide.

For an NSInteger, use %ld and cast the value to long.

[d appendFormat:@"precedence:%ld, ", (long)precedence];
like image 85
Kurt Revis Avatar answered Sep 17 '22 06:09

Kurt Revis