将 (u)int64_t 转换为 NSNumber
所以本质上我的问题是这样的,我正在使用 uint64_t 对象作为键创建一个 NSMutableDictionary 。
还有比这样做更好的方法来创建它们吗?
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 位的,无论我在什么平台上运行它。
我想我可以通过始终使用 unsigned long long 来避免整个问题,但是如果我以任何有效数量分配这些对象,当然会浪费 64 位计算机上的大量堆空间......
So essentially my question is this, I am creating an NSMutableDictionary using uint64_t objects as the key.
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
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.
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
long long
在 64 位 OS X/iOS 平台上是 64 位。在所有 OpenStep 后代平台上,numberWithUnsignedLongLong:
对于uint64_t
是正确的。上次我检查过,您使用的工厂方法实际上并不影响所使用的表示;它仅取决于数字的值(除非您使用的尺寸太小,导致其被截断)。
更新:现在,正确答案是
NSNumber *bobsNumber = @(bob);
。long long
is 64-bit on 64-bit OS X/iOS platforms. On all OpenStep-descended platforms,numberWithUnsignedLongLong:
is correct foruint64_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).
Update: these days, the correct answer is
NSNumber *bobsNumber = @(bob);
.