我应该使用核心数据中的什么 NSNumber (整数 16、32、64)来保留 NSUInteger
我想将 NSUInteger 保留到我的核心数据中,但我不知道应该使用哪种类型(整数 16、32、64)来满足所需的空间。
根据我的理解:
Integer 16 can have minimum value of -32,768 to 32,767
Integer 32 can have minimum value of -2,147,483,648 to 2,147,483,647
Integer 64 can have minimum value of -very large to very large
NSUInteger 是 unsigned long 的 def 类型,等于 unsigned int (输入iPhone 上的 Objective-c),
所以如果我使用 numberWithUnsignedInteger: 将 NSUInteger 转换为 NSNumber 并将其保存为 NSNumber(Integer 32) 我可以安全地检索数据 正确的?
I want to keep NSUInteger into my core data and I don't know which type should I use (integer 16, 32, 64) to suit the space needed.
From my understanding:
Integer 16 can have minimum value of -32,768 to 32,767
Integer 32 can have minimum value of -2,147,483,648 to 2,147,483,647
Integer 64 can have minimum value of -very large to very large
and NSUInteger is type def of unsigned long which equal to unsigned int (Types in objective-c on iPhone)
so If I convert my NSUInteger to NSNumber with numberWithUnsignedInteger: and save it as NSNumber(Integer 32) I could retrieve my data back safely right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您真的需要
NSUInteger
的整个范围吗?在 iOS 上,这是一个无符号 32 位值,可能会变得非常大。它将找到一个有符号的 64 位。但无论如何你可能不需要那么高的精度。
uint32_t
的最大值为UINT32_MAX
,即 4,294,967,295(40 亿)。如果每秒增加一次,则需要超过 136 年才能达到该值。到那时你的用户的 iPhone 就不会出现了...:)Do you really need the entire range of an
NSUInteger
? On iOS that's an unsigned 32 bit value, which can get very large. It will find into a signed 64 bit.But you probably don't need that much precision anyway. The maximum for a
uint32_t
isUINT32_MAX
which is 4,294,967,295 (4 billion). If you increment once a second, it'll take you more than 136 years to reach that value. Your user's iPhone won't be around by then... :)如果可能的话,在将数据写入磁盘或通过网络写入数据时,最好明确说明值的大小。不要使用 NSUInteger 作为数据类型,而是根据您需要的范围使用
uint16_t
、uint32_t
或uint64_t
。然后,这自然会转换为 Core Data 中的整数 16、32 和 64。要了解原因,请考虑以下场景:
NSUInteger
中(使用 NSNumber 的unsignedIntegerValue
)。现在,由于
NSUInteger
在 32 位设备上仅为 32 位,因此该数字不再是 5,000,000,000,因为没有足够的位来表示 50 亿。如果您将步骤 3 中的NUInteger
替换为uint64_t
,那么该值仍将是 50 亿。如果您绝对必须使用 NSUInteger,那么您只需要警惕上述问题并为其编写防御性代码即可。
至于将无符号值存储到看似有符号的核心数据类型中,您可以安全地存储和检索它们:
If at all possible, when writing data to disk or across a network, it's best to be explicit about the size of value. Instead of using NSUInteger as the datatype, use
uint16_t
,uint32_t
, oruint64_t
depending on the range you need. This then naturally translates to Integer 16, 32, and 64 in Core Data.To understand why, consider this scenario:
NSUInteger
(using NSNumber'sunsignedIntegerValue
).Now because
NSUInteger
is only 32-bits on the 32-bit device, the number is no longer 5,000,000,000 because there aren't enough bits to represent 5 billion. If you had swapped theNUInteger
in step 3 foruint64_t
then the value would still be 5 billion.If you absolutely must use NSUInteger, then you'll just need to be wary about the issues described above and code defensively for it.
As far as storing unsigned values into the seemingly signed Core Data types, you can safely store them and retrieve them: