Xcode - 是否可以在 Interface Builder 的实时渲染过程 (IBDesignable) 中调试崩溃?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26368265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - is it possible to debug crashes in the Interface Builder's Live Rendering process (IBDesignable)?
提问by Chris Conover
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.
是否可以设置断点并调试由 Xcode/Interface Builder 的实时渲染执行的代码?也就是说,我正在使用 IBDesignable 和 IBInspectable 属性来影响在界面构建器故事板中呈现的视图,并且希望能够调试“IB 崩溃”错误。除了关于 prepareForInterfaceBuilder 的部分,我不记得在 WWDC 会议上有过这一点。
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.
我最近开始看到许多错误报告 IB 在尝试呈现我的代码时崩溃。我最终将它追溯到我的一个视图中 layoutSubviews 中的一行无害的代码,该视图从故事板链接。IB 的具体问题 - 在渲染实时视图时,在设置所有视图的子视图属性之前调用 layoutSubviews(这本身似乎是一个单独的问题)。这与模拟器和设备形成对比,其中视图是在第一次调用 layoutSubviews 之前设置的。长话短说,它涉及对一堆文件进行比较以找到有问题的更改。我只想说,如果可以设置断点并捕获崩溃,我本可以很快找到问题。
Has anyone figured out how to debug and/or generally trap these problems, or is that access not yet available?
有没有人想出如何调试和/或通常捕获这些问题,或者该访问权限尚不可用?
回答by Chris Conover
I watched the WWDC video again (§411 @22:00 or so). You have to
我再次观看了 WWDC 视频(§411 @22:00 左右)。你必须
- 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)
- 在 IB 中编辑视图,并将其自定义类设置为代码库中的类
- 在自定义类中根据需要设置断点
- 在 IB 中选择视图,然后选择 Editor -> "Debug Selected Views"(底部)
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).
奇怪的是,在我今天的测试中,它通过 (initWith)Frame 而不是 (initWith)Coder 创建我的视图。这会导致后续约束配置逻辑失败,因为尚未设置子视图(因为它们会调用 'withCoder)。
回答by cbowns
(tools note: Xcode 8.2, macOS Sierra)
(工具说明: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:
我遇到了与您截然不同的失败:我的代码崩溃导致 Xcode 中的 IB 错误:
error: IB Designables: Failed to update auto layout status: The agent crashed
错误:IB Designables:无法更新自动布局状态:代理崩溃
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.
要在 macOS Sierra 上进行调试,请打开 Console.app,然后在右侧边栏中打开“用户报告”。找到“IBDesignablesCocoaTouch*”崩溃,然后选择适用的。这为我提供了诊断它所需的代码级信息。
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)
(就我而言,Interface Builder 无法加载图像文字。我必须实施此解决方法:xcode 6 IB_DESIGNABLE- not loading resources from bundle in Interface builder)