asdfkjaasdflf
asdfkjaasdflf

Reputation: 71

swift: correct way to add byte to Data

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)

Upvotes: 4

Views: 2768

Answers (1)

vadian
vadian

Reputation: 285220

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].

Upvotes: 4

Related Questions