When setting up a CoreData entity I can set its type to "Integer 16", "Integer 32" and "Integer 64" which represents Int16, Int32 and Int64 in Swift.
I need to store data that is in range of 0-65535.
Is it really necessary to use "Integer 32" (and waste space) or is it possible to use an UInt16 with Core Data somehow?
Core Data doesn't provide a specific data type for unsigned integers like UInt16. However, you can still represent values in the range of 0–65535 using Integer 16 by adjusting the interpretation of the stored values in your Swift code.
Here's what you can do:
Store as Integer 16: Use the Integer 16 type in your Core Data model, which represents a signed 16-bit integer.
Adjust in Swift Code: In your Swift code, treat the fetched values as if they were unsigned. For example, if you fetch an integer 16 value from Core Data, you can convert it to UInt16 in your Swift code.
This essentially interprets the bits of the signed integer as an unsigned integer. Since you know that your values are within the range of 0-65535, this conversion should work without any issues.
Validation: Ensure that when you store values, they are within the valid range of 0-65535. You can enforce this constraint in your application logic before saving data to Core Data.
Using Integer 32 would indeed be a waste of space if you know that your values will always be within the range of an unsigned 16-bit integer. The described approach allows you to use the more space-efficient Integer 16 type in your Core Data model while handling the unsigned interpretation in your Swift code.