Xcode - is it possible to debug crashes in the Int

2020-05-20 08:18发布

Question

Is it possible to set breakpoints and debug code executed by Xcode / Interface Builder's Live Rendering? That is, I am using the IBDesignable and IBInspectable attributes to affect the view as it is rendered in an interface builder storyboard, and would like to be able to debug "IB crashed" errors. I don't recall this from the WWDC session, other than the section on prepareForInterfaceBuilder.

Context

I recently started seeing a number of errors reporting that IB crashed when attempting to render my code. I ultimately traced it to an otherwise innocent line of code in layoutSubviews in one of my views that was linked from the storyboard. The specific problem that IB - when rendering live views, called layoutSubviews before setting all the view's child view properties (which seems like a separate problem, in and of itself). This contrasted with the simulator and device, in which the views were set ahead of the first call to layoutSubviews. Long story short though, it involved diffing a bunch of files to find the offending change. Suffice to say, I could have found the problem quickly if it were possible to set breakpoints and trap the crash.

Has anyone figured out how to debug and/or generally trap these problems, or is that access not yet available?

2条回答
我命由我不由天
2楼-- · 2020-05-20 09:14

(tools note: Xcode 8.2, macOS Sierra)

I had a very different failure than yours: I had a crash in code that resulted in an IB error in Xcode:

error: IB Designables: Failed to update auto layout status: The agent crashed

To debug this on macOS Sierra, open Console.app, and open "User Reports" in the right sidebar. Find the "IBDesignablesCocoaTouch*" crashes, and select the applicable one. This gave me the code-level info I needed to diagnose it.

Crashed Thread:        0

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [0]

Application Specific Information:
CoreSimulator 375.21 - Device: IBSimDeviceTypeiPad2x - Runtime: iOS 10.3 (14E8301) - DeviceType: IBSimDeviceTypeiPad2x

Thread 0 Crashed:
0   libswiftUIKit.dylib             0x000000021a2ddead _TToFE5UIKitCSo7UIImagecfT24imageLiteralResourceNameSS_S0_ + 205
1   libswiftUIKit.dylib             0x000000021a2ddcef _TFE5UIKitCSo7UIImageCfT24imageLiteralResourceNameSS_S0_ + 79
2   com.company.App         0x0000000216df74ad _TZFC10App21ControlsP33_F7118662B6663018C7DD0DEBB6FDD87511buttonImagefT8forStateOS_25State_CSo7UIImage + 109 (Controls.swift:121)
…

(In my case, Interface Builder was failing to load an image literal. I had to implement this workaround: xcode 6 IB_DESIGNABLE- not loading resources from bundle in Interface builder)

查看更多
等我变得足够好
3楼-- · 2020-05-20 09:16

I watched the WWDC video again (§411 @22:00 or so). You have to

  • edit a view in IB, and set its custom class to a class in your codebase
  • set breakpoints as desired in your custom class
  • select the view in IB, then select Editor -> "Debug Selected Views" (at bottom)

Oddly, in my tests today, it is creating my view via (initWith)Frame instead of (initWith)Coder. This causes subsequent constraint configuration logic to fail as the sub views have not been set (as they would had 'withCoder been called).

查看更多
登录 后发表回答