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:

Essential Developer

Essential Developer: Learn the most up-to-date techniques and strategies for testing new and legacy Swift code in this free practical course for developers who want to become complete senior iOS developers. This virtual event includes lectures, mentoring sessions, and step-by-step instructions. Click to learn more.