swiftxcodemacosdebuggingfoundation

How to inspect the bytes contained in a "Foundation.Data" byte buffer in Xcode?


I'm having trouble getting Xcode to show me the underlying memory of a Foundation.Data.

I'm parsing a binary format that's passed to my function as a blob in a Data. In the debugger Xcode shows me this:

"data" as I can view it in the debug panel

I can control-click on any of those and choose 'View Memory of "data"' etc, but none of them show the memory block I'm looking for. (I know this because I print() the first few bytes accessed via the subscript operator data[0] etc.)

I'm used to languages more like C where I'd have a pointer or array.

I've also tried stepping through the disassembly while viewing the registers where the code is indexing into the data but I'm not used to ARM asm and can't seem to find the address I'm looking for that way either.

(No, it's not the memory at 0x40006000022804b0)


desired behavior

View the bytes encapsulated by the Data

specific problem or error

Every component of the Data that the debugger presents to me shows either nothing or bytes other than what I'm looking for when I use the context menu "View Memory of ..."

and the shortest code necessary to reproduce the problem

This problem is not about my code. It's about how to get the Xcode debugger to show me the underlying memory of an object.


Solution

  • Data has multiple different backing storage strategies (e.g. one for slicing, one for bridging to Objective C, etc.)

    0x40006000022804b0 contains a Foundation.__DataStorage object. It has a _bytes property which is where the actual buffer is. It's exposed -[NSData bytes], which you can print out for debugging purposes