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)

Support Swift by Sundell by checking out this sponsor:


Raycast: Take the macOS Spotlight experience to the next level: Create Jira issues, manage GitHub pull requests and control other tools with a few keystrokes. Easily automate every-day tasks and boost your developer productivity by downloading Raycast for free.