Help with pointers and debugging UIViewAlertForUnsatisfiableConstraints

Hi folks, I'm trying to debug some broken constraints and I stumbled upon a post on medium that showed how to get the memory address of the view with broken constraints and how to change the bg color for easy debugging.

In the post the person show the following code

ex [(UIView *)0x7f94ff5b3890 setBackgroundColor: [UIColor greenColor]]

It changes the background color of the UIView that has a broken constraint to easily identify it on the app.

My goal is to write that code in Swift, since I'm not very familiar with Objective-C and I'd like to know how it looks in Swift.

I've got as far as this (fake address for posting purposes)

(lldb) ex -l swift -- import Foundation
(lldb) ex -l swift -- import UIKit
(lldb) ex -l swift -- UnsafeMutableRawPointer(bitPattern: 0x000)?.load(as: UIView.self)
(UIView?) $R9 = 0x000 {
  UIKit.UIResponder = {
    ObjectiveC.NSObject = {}
  }
}

But if I try to print the backgroundColor or any other property, I get this error:

2023-06-29 11:31:22.644347-0300 APP - DBG[...] +[APP.Button frame]: unrecognized selector sent to class 0x000
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-5)..
The process has been returned to the state before expression evaluation.

Any clues on how to do it or why it is happening?

(Also any sessions regarding this kind of debugging is welcome)

Answered by DTS Engineer in 757668022

In the Objective-C example you posted, 0x7f94ff5b3890 is the address of the UIView itself. So (UIView *)0x7f94ff5b3890 creates a UIView pointer for that object. The equivalent in Swift is unsafeBitCast(0x7f94ff5b3890 to: UIView.self). For example:

(lldb) expr -- unsafeBitCast(0x7f94ff5b3890, to: UIView.self).backgroundColor = UIColor.red
() $R1 = {}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

In the Objective-C example you posted, 0x7f94ff5b3890 is the address of the UIView itself. So (UIView *)0x7f94ff5b3890 creates a UIView pointer for that object. The equivalent in Swift is unsafeBitCast(0x7f94ff5b3890 to: UIView.self). For example:

(lldb) expr -- unsafeBitCast(0x7f94ff5b3890, to: UIView.self).backgroundColor = UIColor.red
() $R1 = {}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Help with pointers and debugging UIViewAlertForUnsatisfiableConstraints
 
 
Q