Articles, podcasts and news about Swift development, by John Sundell.

Converting between String and Data without optionals

Published on 15 Apr 2019
Basics article available: Strings

While 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)