ohho
ohho

Reputation: 51941

%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?

Upvotes: 11

Views: 3871

Answers (2)

CReaTuS
CReaTuS

Reputation: 2585

Try this

UPD:

NSInteger precedence = [self operatorPrecedence];
[d appendFormat:@"precedence:%ld, ", (long)precedence];

Upvotes: -1

Kurt Revis
Kurt Revis

Reputation: 27994

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];

Upvotes: 13

Related Questions