Home Swift Convert String to Data
Post
Cancel

Swift Convert String to Data

While saving application content at rest or sending it with a network request you need to convert text into data, and with a single line of code, you can do that:

1
let stringData = Data(yourString.utf8)

Here, I have used the utf8 encoding, you can choose depending on your content.

And if you want to convert data to string:

1
let string = String(data: yourData, encoding: .utf8)
This post is licensed under CC BY 4.0 by the author.