13

When debugging Objective-C code in LLDB, I often create variables that refer to objects in memory using just their address. For example:

(lldb) po self.view
<UIView: 0x7ff5f7a18430; frame = (0 64; 320 504); autoresize = W+H; layer = <CALayer: 0x7ff5f7a192e0>>
(lldb) e CALayer* $layer = (CALayer*) 0x7ff5f7a192e0
(lldb) e $layer.borderWidth
(CGFloat) $17 = 0

Given just an object's type and its address in memory, I'm able to inspect and manipulate it.

Is this impossible when debugging Swift code?

Bill
  • 44,502
  • 24
  • 122
  • 213

1 Answers1

20
(lldb) e let $layer = unsafeBitCast(0x7fd120f474b0, CALayer.self)
Bill
  • 44,502
  • 24
  • 122
  • 213
Darren
  • 25,520
  • 5
  • 61
  • 71
  • `unsafeBitCast` is even cooler. How did you find out about these APIs? None of them seem to be in the docs. – Bill Jan 19 '15 at 02:19
  • 2
    @Bill I found them by poking around the Swift standard library header (Command+Click on a standard library function or symbol) . All of the interesting functions and classes have "Unsafe" in their name or comments. – Darren Jan 19 '15 at 21:09
  • I posted the updated lldb expression that got me started on Xcode 7.3 here: http://stackoverflow.com/questions/29441418/lldb-swift-casting-raw-address-into-usable-type/38905566#38905566 – sfaxon Aug 11 '16 at 20:31
  • In Swift 3: `e let $layer = unsafeBitCast(0x7fd120f474b0, to: CALayer.self)` – Martin R Oct 06 '16 at 09:52