Xcode 9 & Swift 4 - 呈现视图控制器时意外崩溃

Xcode 9 & Swift 4 - Unexpected Crash When Presenting View Controller

我只有在使用 Xcode 9 Beta 1 和 Swift 4 时才会崩溃。 我正在尝试呈现一个视图控制器,但抛出了一个异常,即使我有几个断点来捕获所有异常 Xcode 不会在任何地方停止。

使用 lldb 我能够打印异常,因为 Xcode 没有这样做。 这是我得到的:

(lldb) po $arg1

-[UIDeviceRGBColor pointSize]: unrecognized selector sent to instance 0x60000067a6c0
(null)

这是堆栈跟踪:

回溯:

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1 5.1
  * frame #0: 0x00000001090e60f1 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x0000000109806494 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    frame #2: 0x00000001096ff858 CoreFoundation`___forwarding___ + 1432
    frame #3: 0x00000001096ff238 CoreFoundation`__forwarding_prep_0___ + 120
    frame #4: 0x0000000112e9f350 UIFoundation`__NSStringDrawingEngine + 3564
    frame #5: 0x0000000112ea32b2 UIFoundation`-[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 787
    frame #6: 0x000000010a84e96e UIKit`-[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 1022
    frame #7: 0x000000010a84e33b UIKit`-[UILabel textRectForBounds:limitedToNumberOfLines:] + 68
    frame #8: 0x000000010a8543c9 UIKit`-[UILabel _intrinsicSizeWithinSize:] + 167
    frame #9: 0x000000010b242b24 UIKit`-[UITextField _intrinsicSizeWithinSize:] + 238
    frame #10: 0x000000010b0b61ab UIKit`-[UIView(UIConstraintBasedLayout) intrinsicContentSize] + 37
    frame #11: 0x000000010b0b6a00 UIKit`-[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 35
    frame #12: 0x000000010b0b6643 UIKit`-[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 262
    frame #13: 0x000000010b0c0fef UIKit`-[UIView(AdditionalLayoutSupport) _updateSystemConstraints] + 99
    frame #14: 0x000000010b0bf8b7 UIKit`-[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 161
    frame #15: 0x000000010b0bfebf UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1286
    frame #16: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #17: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #18: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #19: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #20: 0x0000000107d1d1a1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
    frame #21: 0x000000010b0c06f0 UIKit`__100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 90
    frame #22: 0x000000010b0bef7e UIKit`-[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
    frame #23: 0x000000010b0c025f UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 160
    frame #24: 0x000000010b0c134f UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 344
    frame #25: 0x000000010a66f414 UIKit`-[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
    frame #26: 0x000000010a599e09 UIKit`-[UIPickerView layoutSubviews] + 189
    frame #27: 0x000000010a59d697 UIKit`-[UIPickerView selectedRowInComponent:] + 45
    frame #28: 0x000000010a5982fc UIKit`-[UIPickerView _updateSelectedRows] + 143
    frame #29: 0x000000010a598396 UIKit`-[UIPickerView didMoveToWindow] + 101
    frame #30: 0x000000010a67a8e1 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 1701
    frame #31: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #32: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #33: 0x000000010a692e45 UIKit`-[UIScrollView _didMoveFromWindow:toWindow:] + 84
    frame #34: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #35: 0x000000010a66d259 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 151
    frame #36: 0x000000010a66d140 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 828
    frame #37: 0x000000010a67d4ad UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1917
    frame #38: 0x000000010a71a82b UIKit`-[UITransitionView transition:fromView:toView:removeFromView:] + 1541
    frame #39: 0x000000010b2c9f29 UIKit`-[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 1712
    frame #40: 0x000000010a720845 UIKit`__56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 3003
    frame #41: 0x000000010a5c46cc UIKit`_runAfterCACommitDeferredBlocks + 318
    frame #42: 0x000000010a5b2cef UIKit`_cleanUpAfterCAFlushAndRunDeferredBlocks + 280
    frame #43: 0x000000010a5e3091 UIKit`_afterCACommitHandler + 137
    frame #44: 0x000000010971f507 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #45: 0x000000010971f45e CoreFoundation`__CFRunLoopDoObservers + 430
    frame #46: 0x00000001097038f4 CoreFoundation`__CFRunLoopRun + 1332
    frame #47: 0x0000000109703149 CoreFoundation`CFRunLoopRunSpecific + 409
    frame #48: 0x000000011182c9d7 GraphicsServices`GSEventRunModal + 62
    frame #49: 0x000000010a5b891d UIKit`UIApplicationMain + 159
    frame #50: 0x00000001076a1cc7 GradePoint`main at AppDelegate.swift:13
    frame #51: 0x000000010d224b65 libdyld.dylib`start + 1

我知道这与无法识别的选择器有关,但我没有对 UIDeviceRGBColorPointSize 做任何事情。

不太确定会发生什么,当我切换到 Xcode 8 时一切正常。

如果您想复制,请随意克隆项目 here 并切换到 features/ios11 分支。您所要做的就是完成介绍屏幕,然后单击 + 按钮。崩溃将在之后立即发生。 运行 使用 Xcode 9 测试版 1

我知道这可能只是一个测试版错误,但如果有人对此有更多经验,我会很感激一些帮助,如果没有,至少帮助如何提交 Xcode 错误报告,因为我'我从来没有做过。

谢谢!

我尝试下载您的代码并重现崩溃,但我看不到崩溃。我已经在 iPhone 7 Plus 模拟器和我的 iPhone 6S 上尝试了 运行 你的代码,它在两个地方都运行良好。 (我是 运行 你在 Xcode 9 Beta 1 上的项目)

您能否分享有关您 运行 代码所处环境的更多详细信息?也许是您正在使用的 Swift 版本设置(Select 您的目标 > 构建设置 > Swift 语言版本)或者您是否执行了 Swift 4 迁移?我很乐意进一步调查。

同时,here's how to file a bug report with Apple.

[我知道此内容应该作为评论而不是答案发布,但在撰写本文时我没有足够的声誉点数来做这件事。]

UIDeviceRGBColor 在技术上是 UIColorpointSizeUIFont 上的一个方法。执行 UILabel.

的布局时会发生此错误

最可能的原因是您将 UIColor 实例分配给 UILabel.font

如果你有一个属性字符串,它会导致例如按属性:

let attributes: [String: Any] = [NSFontAttributeName: UIColor.black]

请注意,由于属性采用 Any 值,因此没有类型检查,这可能发生在 Swift 中。