我应该使用核心数据中的什么 NSNumber (整数 16、32、64)来保留 NSUInteger

发布于 2024-12-27 01:11:57 字数 587 浏览 2 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2025-01-03 01:11:57

真的需要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 is UINT32_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... :)

迎风吟唱 2025-01-03 01:11:57

如果可能的话,在将数据写入磁盘或通过网络写入数据时,最好明确说明值的大小。不要使用 NSUInteger 作为数据类型,而是根据您需要的范围使用 uint16_tuint32_tuint64_t。然后,这自然会转换为 Core Data 中的整数 16、32 和 64。

要了解原因,请考虑以下场景:

  1. 您选择使用 Integer 64 类型来存储您的值。
  2. 在 64 位 iOS 设备(例如 iPhone 6)上,它存储值 5,000,000,000。
  3. 在 32 位 iOS 设备上,此值从存储中提取到 NSUInteger 中(使用 NSNumber 的 unsignedIntegerValue)。

现在,由于 NSUInteger 在 32 位设备上仅为 32 位,因此该数字不再是 5,000,000,000,因为没有足够的位来表示 50 亿。如果您将步骤 3 中的 NUInteger 替换为 uint64_t,那么该值仍将是 50 亿。

如果您绝对必须使用 NSUInteger,那么您只需要警惕上述问题并为其编写防御性代码即可。

至于将无符号值存储到看似有符号的核心数据类型中,您可以安全地存储和检索它们:

NSManagedObject *object = // create object
object.valueNumber = @(4000000000); // Store 4 billion in an Integer 32 Core Data type
[managedObjectContext save:NULL] // Save value to store

// Later on
NSManagedObject *object = // fetch object from store
uint32_t value = object.valueNumber.unsignedIntegerValue; // value will be 4 billion

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, or uint64_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:

  1. You opt to use Integer 64 type to store your value.
  2. On a 64-bit iOS device (eg iPhone 6) it stores the value 5,000,000,000.
  3. On a 32-bit iOS device this value is fetched from the store into an NSUInteger (using NSNumber's unsignedIntegerValue).

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 the NUInteger in step 3 for uint64_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:

NSManagedObject *object = // create object
object.valueNumber = @(4000000000); // Store 4 billion in an Integer 32 Core Data type
[managedObjectContext save:NULL] // Save value to store

// Later on
NSManagedObject *object = // fetch object from store
uint32_t value = object.valueNumber.unsignedIntegerValue; // value will be 4 billion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文