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?

asked Jan 18, 2015 at 23:57

1 Answer 1

20
(lldb) e let $layer = unsafeBitCast(0x7fd120f474b0, CALayer.self)
Bill
46k25 gold badges128 silver badges218 bronze badges
answered Jan 19, 2015 at 0:35
Sign up to request clarification or add additional context in comments.

4 Comments

unsafeBitCast is even cooler. How did you find out about these APIs? None of them seem to be in the docs.
@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.
I posted the updated lldb expression that got me started on Xcode 7.3 here: stackoverflow.com/questions/29441418/…
In Swift 3: e let $layer = unsafeBitCast(0x7fd120f474b0, to: CALayer.self)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.