Is there a way to store an unsigned long in the main data?

CoreData provides Integer 16, Integer 32, and Integer 64 repositories, but does not support any qualifiers. You can store unsigned int (32 bits) as a signed long (64 bits) and make sure that the value is stored for the full range, but for unsigned long, apparently a 128-bit signed integer is required for storage, which of course , t supported by CoreData. Is there a way to save an unsigned long in coreData?

+4
source share
4 answers

[Previous comment advancing to reply]

It seems like this is a bitmap that is important to you, and not an integer value as such. You can save it as a signed one - just drop it, because C signed ↔ unsigned casts does not apply mathematical correctness and just saves bits. Bring it back to use it.

Next question:

In the general case, yes (Obj-) C (++) you can save an unsigned integer into a variable with an equivalent type with a signed integer type and vice versa. C differs from signed → unsigned by definition, equates to a bit copy when using 2 integers and two types are the same size. In other words, unsigned -> signed, is an "implementation defined", which in practice usually means a bit copy. Clang and GCC use a bit copy for both, but if you want to be absolutely sure, you can use union :

 unsigned long r; long l; r = (unsigned long)l; // will always work (cast optional) // following is l = (long)r (cast optional) without "implementation defined" risk { union { long sValue; unsigned long uValue; } tmp; tmp.uValue = r; l = tmp.sValue;} 

But seriously, I doubt anyone will be! (Note: Clang will at least compile it to its direct destination (bit copy).)

+3
source

If you really need the full precision of unsigned 64-bit, you can make it transformable (check the documentation about saving Custom Permanent Attributes ). CoreData allows you to store something like that. But you probably don't need full 64-bit precision ...?!?

+2
source

You can always convert [de] to serialize it as a string. This is not particularly clean, but it gives you the ability to store it until you can re-process it without a sign.

0
source

An unsigned long not 128 bits (yet).
(or do you have a 128-bit processor?)

On a Mac, depending on the architecture of your processor, it may be 32 or 64 bits.

Watch with

 NSLog( @"%u", sizeof( unsigned long ) ); 

Thus, in principle, an unsigned long will be compatible with Integer32 or Integer64 .

0
source

All Articles