将(u)int64_t转换为NSNumbers [英] Converting (u)int64_t to NSNumbers

查看:865
本文介绍了将(u)int64_t转换为NSNumbers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,本质上我的问题是这是,我创建一个NSMutableDictionary使用uint64_t对象作为关键。

So essentially my question is this, I am creating an NSMutableDictionary using uint64_t objects as the key.

有没有更好的方式来创建它们? / p>

Is there any better way to create them than doing this?

uint64_t bob=7;

NSNumber *bobsNumber;

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
bobsNumber=[NSNumber numberWithUnsignedLong:bob];
#else
bobsNumber=[NSNumber numberWithUnsignedLongLong:bob];
#endif

这将工作,只要你没有包括在二进制文件/套接字/ NSData对象/任何。但是有没有更好的方法这样做?我真的想确保对象是64位,而不管我运行它的平台。

This would work as long as you didn't include it in a binary file/sockets/NSData object/whatever. But is there any better way of doing this? I really would like to be sure that the object is 64-bits regardless of what platform I run it on.

我想我可以避免整个问题,总是去unsigned long long但当然,如果我分配这些对象在任何有效的数字,64位机器上浪费吨堆空间....

I guess I could just avoid the whole issue by always going unsigned long long but of course that wastes tons of heap space on 64 bit machines if I allocate these objects in any significant numbers....

推荐答案

在64位OS X / iOS平台上,

long long 为64位。在所有OpenStep下降的平台上, numberWithUnsignedLongLong:适用于 uint64_t

long long is 64-bit on 64-bit OS X/iOS platforms. On all OpenStep-descended platforms, numberWithUnsignedLongLong: is correct for uint64_t.

上次我检查,你使用哪个工厂方法实际上不影响使用的表示;它只依赖于数字的值(除非你使用太小的大小,导致它被截断)。

Last time I checked, which factory method you use doesn’t actually affect the representation used anyway; it’s only dependent on the value of the number (unless you use a too-small size, causing it to be truncated).

更新:这些天,正确的答案是 NSNumber * bobsNumber = @(bob);

Update: these days, the correct answer is NSNumber *bobsNumber = @(bob);.

这篇关于将(u)int64_t转换为NSNumbers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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