问题描述
我在其中使用的NSLog / NSAssert等电话的字符串格式说明相当数量的%D 和%U
与 NSInteger的(= INT上32位)
和 NSUInteger(= unsigned int类型在32位)
分别类型。
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%禄
预计什么,现在变成了一个长
和无符号长
键入
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位符,铸造的64位值类型随处可见的警告是由于在32位版本。
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.
不过,我想知道是否可能有格式说明专为 NSInteger的
和 NSUInteger
类型,将工作这两种架构无需进行转换?
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
这拳通常没有创建新的对象,感谢的魔法。
This boxing doesn't usually have to create a new object thanks to tagged pointer magic.
如果您的真正的不想使用的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位环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!