%d格式化为32位和64位

时间:2013-01-23 04:23:47

标签: ios objective-c xcode macos

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

给出:

  

警告:Format指定类型'int'但参数具有类型   'NSInteger'(又名'长')

和Xcode建议将%d更改为%ld

但是,它仅适用于32位 64位目标,因为NSInteger是:

 #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

对于32位和64位目标,杀死警告的最佳方法是什么?

2 个答案:

答案 0 :(得分:12)

关注the instructions in Apple's 64-Bit Transition Guide

对于NSInteger,请使用%ld并将值转换为long

[d appendFormat:@"precedence:%ld, ", (long)precedence];

答案 1 :(得分:-1)

试试这个

<强> UPD:

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