Converting between String and Data without optionals
Basics article available: StringsWhile Swift offers many APIs for converting between Data
and String
, these two let us do so without any optionals — usually resulting in simpler code:
// String -> Data
let data = Data("Hello, world!".utf8)
// Data -> String
let string = String(decoding: data, as: UTF8.self)