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:


RevenueCat: Easily build and manage iOS and Android in-app purchases. With just a few lines of code RevenueCat provides IAP infrastructure, customer analytics, data integrations, and gives you time back from dealing with edge cases and updates across all platforms.