0

How does one cast a pointer to a known Swift object type in lldb when debugging an iOS app in Xcode 9?

I am trying to print an object's description in Swift given its memory address in Xcode 9 / iOS 11 SDK. I had been using the technique described here: LLDB (Swift): Casting Raw Address into Usable Type (Note: this technique works fine when debugging a macOS app.)

(lldb) p unsafeBitCast(0x00006080000e2280, to: UIViewController.self)

But typing a similar statement in lldb in Xcode 9 while debugging an iOS app yields the following error message:

error: use of undeclared identifier 'to'. Or removing the "to:" part:

error: use of undeclared identifier 'unsafeBitCast'.

I searched for documentation of a replacement for the Swift unsafeBitCast() method, but I have found no mention of its deprecation. Is this a bug in lldb / Swift iOS runtime?

Broken: Xcode 9 + iOS 11 Simulator, Xcode 9 + iOS 9.x Simulator, Xcode 9 + iOS 9.x device
Working: Xcode 8 + iOS Simulator/Device, Xcode 9 + macOS 10.12 app

Thanks for your insights.

Bill Feth
  • 313
  • 3
  • 10
  • For some reason, yesterday Xcode 9 was not cooperating. Calling unsafeBitCast() did not work! Today it worked when debugging an app on an iOS 9.x device, an iOS 11 simulator, and on macOS 10.12.x. The differentiator with this question was that I was having problems in Xcode 9, which is in Beta as of this writing. The question I had referenced and the one @matt duped this to had been answered as of earlier versions of Xcode. – Bill Feth Sep 16 '17 at 04:18
  • Perhaps I was mistaken in my comment above about this working on iOS. Today lldb will not invoke unsafeBitCast() when debugging an app in the iOS 11 Simulator, iOS 9 Simulator, or iOS 9 device in Xcode 9. – Bill Feth Sep 20 '17 at 16:42

1 Answers1

0

You can create an UnsafeMutablePointer from your memory address and retrieve the object using the pointers pointee attribute:

let object = UnsafeMutablePointer<NSTabViewController>(bitPattern: yourMemoryAddress)?.pointee
Palle
  • 11,511
  • 2
  • 40
  • 61