"printing" Foundation.Data

print(Data(hash1))
print(Data(hash1) as NSData)

hash1 in this case being [UInt8], output is:

20 bytes                                      
<7110eda4 d09e062a a5e4a390 b0a572ac 0d2c0220>

Why does printing of a Data item just print the number of bytes instead of a representation of the value. Does anyone find this truly useful?

I do. In general, but especially if the Data is member of another struct which I'm printing, I don't want to be greeted by potentially megabytes of hex output.

Maybe a good compromise would be to show the hex representation only if it's reasonably short, but if we want to keep it simple, I'd rather have the count (although something along the lines of Data(count: 20) might read nicer than just 20 bytes).

2 Likes

I agree that printing just the size isn't very useful for debugging, but is great for general logging, and it's good to be conservative in these cases. However, Swift should provide built in methods for printing Data in various formats, like hex strings, without having to remember the magical formatting incantation to do so.

5 Likes

Isn't that some of the capabilities being discussed in the Custom String Interpolation thread?

I had asked about that some time ago ( No hex dump anymore in Data description ) and filed a bug report ( SR-2514 – Add a "hex dump" method to Data ), but I admit that I did not pursue the issue.

Terms of Service

Privacy Policy

Cookie Policy