Convert Data to String in Swift

In this quick Swift tutorial, I wanted to share how to convert a Data object into String. This is very helpful when you want to print out in Xcode console the response string returned in the body of the HTTP response. This is of course if you know the HTTP response body contains a JSON/text/XML rather than an image for example.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Convert Data object to String

let stringValue = String(decoding: data, as: UTF8.self)
print(stringValue)

I hope this little Swift code snippet will be of some value to you!

 

Leave a Reply

Your email address will not be published. Required fields are marked *