What NSNumber (Integer 16, 32, 64) in Core Data should I use to keep NSUInteger

前端 未结 2 1182
长情又很酷
长情又很酷 2020-12-14 05:23

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:



        
相关标签:
2条回答
  • 2020-12-14 05:48

    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
    
    0 讨论(0)
  • 2020-12-14 06:08

    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... :)

    0 讨论(0)
提交回复
热议问题