How to view Data? – NSData is truncated now

In No hex dump anymore in Data description and SR-2514 I had reported that there is no native method to view the (full) contents of a Data value, and that a bridge to NSData can be used as a workaround.

Apparently that workaround does not work anymore in Xcode 11 beta 2, the description of NSData is truncated now:

let data = Data(1...255)
print(data)
// 255 bytes
print(data as NSData)
// {length = 255, bytes = 0x01020304 05060708 090a0b0c 0d0e0f10 ... f8f9fafb fcfdfeff }

Is this truncation of the NSData output an intentional change? Are there other methods now to print a full hex dump, or do we have to define our own method?

Regards, Martin

The change to NSData is probably intended; relying on the format of description never changing is fragile.

In terms of going from here, Data's description should mirror NSData in my opinion, a byte count alone is not very useful. There is also room for a hexadecimalString API that returns the data as hex; it is very commonly required and would warrant Foundation inclusion IMO.

1 Like

Just to clarify: What I had in mind is a hex dump for debugging purposes (such as: β€œWhat data did the server send exactly?”, β€œWhy does the conversion from Data to String fail?”), not a reliable format for further processing.

Gotcha. Again, I think the truncated form currently used by NSData is a good balance, and then a separate property/method which generates a full string is there if you need it (for production or debugging purposes).

I did some more research: The truncation of the NSData output happens on macOS 10.15 (beta), but not on on 10.14.5, and also in an Objective-C program. So this seems to be a change in the Foundation library on Catalina, and unrelated to Swift.

It remains the question if Swift's Data should get a hexEncodedString() method.

It could be lengthy, but since this is for debugging purpose, I do use dump(data).

That is a good tip. It requires bridging to NSData:

let data = Data(1...255)
dump(data)

/*
β–Ώ 255 bytes
  - count: 255
  β–Ώ pointer: 0x00000001005a2240
    - pointerValue: 4300874304
*/

dump(data as NSData)

/*
β–Ώ <01020304 05060708 090a0b0c 0d0e0f10 11121314 15161718 191a1b1c 1d1e1f20 21222324 25262728 292a2b2c 2d2e2f30 31323334 35363738 393a3b3c 3d3e3f40 41424344 45464748 494a4b4c 4d4e4f50 51525354 55565758 595a5b5c 5d5e5f60 61626364 65666768 696a6b6c 6d6e6f70 71727374 75767778 797a7b7c 7d7e7f80 81828384 85868788 898a8b8c 8d8e8f90 91929394 95969798 999a9b9c 9d9e9fa0 a1a2a3a4 a5a6a7a8 a9aaabac adaeafb0 b1b2b3b4 b5b6b7b8 b9babbbc bdbebfc0 c1c2c3c4 c5c6c7c8 c9cacbcc cdcecfd0 d1d2d3d4 d5d6d7d8 d9dadbdc dddedfe0 e1e2e3e4 e5e6e7e8 e9eaebec edeeeff0 f1f2f3f4 f5f6f7f8 f9fafbfc fdfeff> #0
  - super: NSData
...
*/
1 Like

Right. dump treat Data as array of UInt8 (But it knows enough to call them bytes?). Good catch.

If data is a collection, then if I will get a data from a bitmap, via the pngRepresetation, will its count (data.count) be exactly the same as count of bytes, which the png bitmap occupies on memory?
UPD. Answer is yes