Converting between String and Data without optionalsBasics article available: Strings
While Swift offers many APIs for converting between
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)
This ad keeps all of Swift by Sundell free for everyone. If you can, please check this sponsor out, as that directly helps support this site:
Bitrise: My favorite continuous integration service. Automatically build, test and distribute your app on every Pull Request — which is perfect for teams that are now working remotely, as you’ll quickly get feedback on each change that you make. Try out their new, improved free tier to get started.