为什么只有 NSLog 会警告我对 NSUInteger 使用 %lu 字符串格式说明符?
由于某种原因,当我尝试执行以下操作时出现编译错误:
NSLog(@"row: %lu", indexPath.row);
其中 row
的类型为 NSUInteger
。我得到的错误是
转换指定类型“unsigned long”,但参数类型为“NSUInteger”(又名“unsigned int”)
我可以执行以下操作而不会出现编译错误:
NSString * string = [NSString stringWithFormat:@"row: %lu", indexPath.row];
在两种情况下我都使用完全相同的格式字符串和替换参数,但为什么当 -stringWithFormat:
看起来很完美时,NSLog
是否会感到害怕?我的编译器是LLVM 1.6。
For some reason, I get a compilation error when I try to do the following:
NSLog(@"row: %lu", indexPath.row);
where row
is of type NSUInteger
. The error I get is
Conversion specifies type 'unsigned long' but the argument has type 'NSUInteger' (aka 'unsigned int')
I can do the following with no compilation errors:
NSString * string = [NSString stringWithFormat:@"row: %lu", indexPath.row];
I'm using the exact same format string and substitution argument in both cases, but why does NSLog
freak out while -stringWithFormat:
seems to be perfectly content? My compiler is LLVM 1.6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前运行 iOS 的所有设备都是 32 位的。如果您想消除警告:
[编辑:从 iPhone 5s 开始,iOS 始终是 32 位的说法不再正确。]
All devices that iOS currently runs on are 32-bit. If you want to silence the warning:
[Edit: As of the iPhone 5s, it is no longer true that iOS is always 32-bit.]
我遇到了同样的问题,虽然 @Wevah 是正确的并且他的答案工作得很好,但还有另一个选项不需要任何代码更改。有关详细信息,请参阅以下 Apple 文档:
字符串编程指南 |平台依赖项
Cocoa 的 64 位转换指南 |构建像 64 位一样的 32 位
NS_BUILD_32_LIKE_64
预处理器宏非常有用。您可以在 Xcode 项目设置中(在GCC_PREPROCESSOR_DEFINITIONS
下)进行设置,或者将#define NS_BUILD_32_LIKE_64 1
放入预编译头 (.pch) 文件中。在我的应用程序中,这消除了 11 个警告,而无需更改任何代码。这是可行的,因为
unsigned int
和unsigned long
在 iOS 上大小相同(4 字节),因此更改NSUInteger
的 typedef 会使编译器 (和开发人员)很高兴,但硬件并不关心,因为在这两种情况下它只是进行整数数学运算。 :-)I've run into this same issue, and although @Wevah is correct and his answer works just fine, there's another option that doesn't require any code changes. See the following Apple documentation for details:
String Programming Guide | Platform Dependencies
64-Bit Transition Guide for Cocoa | Building 32-Bit Like 64-Bit
The
NS_BUILD_32_LIKE_64
preprocessor macro is quite helpful. You can set it in your Xcode project settings (underGCC_PREPROCESSOR_DEFINITIONS
) or just put#define NS_BUILD_32_LIKE_64 1
in your precompiled header (.pch) file. In my app, this eliminated 11 warnings without any code changes.This works because
unsigned int
andunsigned long
are the same size (4 bytes) on iOS, so changing the typedef ofNSUInteger
makes the compiler (and the developer) happy, but the hardware doesn't care, since it's just doing integer math in both cases. :-)Apple 文档建议使用 %lu 和 %ld 将 64 位值转换为 32 位值。如果您实际使用额外的 32 位,这就会产生问题。格式字符串 %qu 和 %qd 指定 64 位值(分别为无符号和有符号)。如果您希望代码能够在任一模式下编译,则必须将参数列表中声明为 NSUInteger 或 NSInteger 的值强制转换为 UInt64 或 SInt64 以避免出现警告。
Apple documentation recommends casting the 64 bit value to a 32 bit value using %lu and %ld. This poses a problem if you actually use the extra 32 bits. Format strings %qu and %qd specify a 64 bit value (unsigned and signed respectively). If you want code that will compile in either mode, then values declared as NSUInteger or NSInteger will have to be cast to a UInt64 or SInt64 in the parameter list to avoid the warning.