NSCoder for unsigned integers with swift

62 Views Asked by At

In my swift app, I really need to store a UInt32 value and retrieve it using NSCoder. The issue is, there are methods for signed integers :

coder.decodeInteger()
coder.decodeInt32()
coder.decodeInt64() 

but not for unsigned integers. Also, casting an Int32 of negative value in an UInt32 does not seem to work.

Am I missing some point?

1

There are 1 best solutions below

0
Rob Napier On

The tool you want is init(bitPattern:). This is the same as C-style casting on integers (which is how you use these methods in ObjC when you need unsigned ints). It just reinterprets the bits.

UInt32(bitPattern: coder.decodeInt32())