1

Given the infamous < NSLayoutConstraint:0x1c809d970 UIView:0x11fe288b0.height == 16 (active)> & co from the autolayout engine

what's the brave new swiftly world way to print an object of UIView class by a hex pointer value?

(lldb) p *((UIView*)0x11fe288b0)

error: :3:12: error: expected ',' separator *((UIView*)0x11fe288b0) ^ ,

(lldb) po *((UIView*)0x11fe288b0)

error: :3:12: error: expected ',' separator *((UIView*)0x11fe288b0) ^ ,

(lldb) expr *((UIView*)0x11fe288b0)

error: :3:12: error: expected ',' separator *((UIView*)0x11fe288b0) ^ ,

This is in xcode 10.3 and 11 beta 3

UPD:

(lldb) p *((UIView*)0x11fe288b0)

worked at first in xcode 11 beta 5, now it ceased to work: this might have been fixed in beta 5 but possibly that fix got clobbered when I went back to xcode 10.3 that "installed additional components"

UPD2: this seems to be a duplicate of Xcode debugger (lldb) get object description from memory address

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66

2 Answers2

6

You can cast from an address to a UIView (or mutatis mutandis for any other class) in Swift using unsafeBitCast like:

(lldb) po unsafeBitCast(<ADDRESS>, to: UIView.self)

You can also access properties this way:

(lldb) po unsafeBitCast(<ADDRESS>, to: UIView.self).bounds

etc...

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • (lldb) po unsafeBitCast(0x1021a8410, to: UIView.self) error: use of undeclared identifier 'to' – Anton Tropashko Jul 31 '19 at 10:16
  • That's odd. "to:" has been the name for the type parameter in unsafeBitCast since at least swift4.0. If you can repro this in a small example, please file a bug with http://bugs.swift.org. – Jim Ingham Jul 31 '19 at 19:34
  • 1
    Ah, I see. I get the same error you showed if I run this expression in a C frame, since that's the best the C parser can do with the expression. lldb chooses the default language for expressions from the language of the currently selected frame. If you are in a C-frame, use the expression from the previous answer. Only use this one if a swift frame is the currently selected one. – Jim Ingham Jul 31 '19 at 19:37
4

If the problem is just layout constraints, the simplest solution is to give your constraint a string identifier. You can do that in code or in the storyboard. Makes debugging constraints really easy; the identifier appears in quotes first thing in the debug output:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
(
    "<NSLayoutConstraint:0x600003af86e0 'getStartedBad' ... >",
    "<NSLayoutConstraint:0x600003af8730 'getStartedTop' ... >"
)

If the question is about a UIView for which you have only the address, you can send messages to it like this:

(lldb) expr -l objc -O -- [(UIView*)0x11fe288b0 layer]

(Yes, you have to talk Objective-C to it, but I don't think there's any other way.)

matt
  • 515,959
  • 87
  • 875
  • 1,141