2

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?

Anco34
  • 29
  • 1
  • How about converting the signed integers to unsigned just after decoding, and converting the unsigned integers to signed ones just before encoding? – Sweeper Jun 30 '22 at 20:32

1 Answers1

0

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())
Rob Napier
  • 286,113
  • 34
  • 456
  • 610