In the previous post I wrote a few lines of code to convert an NSString object to an NSData object. In this post I’ll do the opposite, putting the conversion into the context how to print an NSData object to the debugging console.
By default, when you print an NSData object to the console using NSLog, the output will be in hex, it will look something like the following:
<7b0a2020 22417272 61792220 3a205b0a 20202020 …>
Depending on what the data consists of, it can be quite useful to see the data as text. As an example, I recently needed to verify that I had properly serialized a dictionary into JSON, viewing the output as text provided the information I was after.
In order to view the NSData object as text you need to convert the object to an NSString, here is a one liner to do that: