I want to convert an (at least I think) array of UInt8 values to Data.
Using Data(bytes: variable) does not work here.
Here's the type of the variable:
po type(of: obj.variable)
(Swift.UInt8, Swift.UInt8, Swift.UInt8, Swift.UInt8)
It seems that this isn't an array of UInt8 but what type is it and how can I convert it to a Data?
Thanks!
The type of
objis a tuple of fourUInt8values.You can access elements of the tuple as follows:
As
Datais effectively aCollectionof bytes, it can be initialized from a sequence ofUInt8values.So the easiest solution would just be to create an
Arrayfrom the elements of the tuple and initialize aDatavalue from it:This is however not the most general solution and only works when the tuple only contains
UInt8values.A more general approach would be to convert the tuple to an
UnsafePointerfirst and create aDatavalue from it: