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

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

问题描述

我在其中使用的NSLog / NSAssert等电话<​​code>的字符串格式说明相当数量的%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位环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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