NSInteger 和 NSUInteger 在混合 64 位/32 位环境中 [英] NSInteger and NSUInteger in a mixed 64bit / 32bit environment

查看:37
本文介绍了NSInteger 和 NSUInteger 在混合 64 位/32 位环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NSLog/NSAssert 等调用中有相当数量的字符串格式说明符,它们使用 %d%uNSInteger (= int on 32bit)NSUInteger (= unsigned int on 32bit) 类型分别.

I have a fair amount of string format specifiers in NSLog / NSAssert etc. calls which use %d and %u with NSInteger (= int on 32bit) and NSUInteger (= unsigned int on 32bit) types respectively.

当将应用程序转换为 64 位时,这会发出警告(当然),因为 %ld %lu 预计现在变成了 longunsigned长类型.

When converting the app to 64bit, this gives warnings (of course), as %ld %lu is expected for what now became a long and unsigned long type.

简单地转换格式说明符当然会在 32 位构建中引入反向警告.
因此,我看到的唯一没有警告的解决方案是使用 64 位说明符,并在 32 位构建中给出警告的任何地方强制转换为 64 位值类型.

Simply converting the format specifiers will of course introduce the reverse warnings in the 32bit build.
So the only solution I see to become warning free is using the 64bit specifiers, and casting to the 64bit value types everywhere a warning is given in the 32bit build.

但我想知道是否有专门针对 NSIntegerNSUInteger 类型的格式说明符,它们可以在两种架构上都适用而无需强制转换?

But I was wondering if perhaps there are format specifiers specifically for the NSInteger and NSUInteger type which would work on both architectures without casting?

推荐答案

我认为最安全的方法是将它们装箱到 NSNumber 实例中.

I think the safest way is to box them into NSNumber instances.

NSLog(@"Number is %@", @(number)); // use the highest level of abstraction

由于 标记指针魔法.

如果你真的不想使用NSNumber,你可以手动转换原始类型,就像其他人建议的那样:

If you really don't want to use NSNumber, you can cast primitive types manually, as others suggested:

NSLog(@"Number is %ld", (long)number); // works the same on 32-bit and 64-bit

这篇关于NSInteger 和 NSUInteger 在混合 64 位/32 位环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆