Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: correct way to add byte to Data

Tags:

swift

nsdata

I've seen some strange gymnastics involving MemoryLayout and arrays when people want to add a plain old byte to a Data/NSData. What is the current accepted practice? Can I not just so something like this?

var myData = Data()
let value: UInt8 = 5
myData.append( value)
like image 725
asdfkjaasdflf Avatar asked Oct 14 '25 16:10

asdfkjaasdflf


1 Answers

Yes you can. Your syntax is correct and valid.

Data conforms to MutableCollection, it can be treated as an array of UInt8 values and it's pretty easily convertible from and to [UInt8].

like image 167
vadian Avatar answered Oct 17 '25 09:10

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!