[Discussion] Bag of Bytes Types

I use Data for bags-of-bytes; I’m not entirely sure why I chose it over [UInt8]. Probably

  • I was used to NSData from Obj-C
  • It ‘feels’ more opaque than an array, less focused on the bytes themselves (kind of like RawSpan vs Span<UInt8>)
  • It’s easy to convert to/from String

The fact that it’s not in the core library is somewhat awkward since I’m developing cross platform code, but my code needs FoundationEssentials for other reasons anyway.

FWIW: String(validating:as:) and String(decoding:as:) are the generic works-on-any-collection stdlib versions of String(data:encoding:)

3 Likes