swift - Xcode 13 beta 5 错误 : UIViewController is missing its initial trait collection populated during initialization

标签 swift xcode uitraitcollection xcode13

在 Xcode 13 beta 5 构建之前,应用程序运行良好。
在我们的 View Controller 中的这段 init 代码行中突然出现此错误:

init(dataProvider: DataProvider) {
    self.dataProvider = dataProvider
    super.init(style: .plain)
    dataProvider.delegate = self
}
这段代码在 Xcode 12 中运行良好,现在没有任何更改,它会中断:

'NSInternalInconsistencyException', reason: 'UIViewController is missing its initial trait collection populated during initialization. This is a serious bug, likely caused by accessing properties or methods on the view controller before calling a UIViewController initializer. View controller: <UITableViewController: 0x7f7fbd291bb0>'


我用谷歌搜索,只有 found 1 obscure thread在这个问题上,但没有多大帮助。
有时设置断点并进入允许它在不崩溃的情况下工作,所以引擎盖下似乎存在某种布局竞争条件,但我正在努力调试。我试过 sleep ,但这不起作用。
任何和所有有关如何调试的建议表示赞赏。现在我无法获得其他信息以进行调试。如果您知道可能导致这种情况的原因或在哪里查看,我将不胜感激。
这是错误堆栈跟踪:
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff203fc8a8 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff2019ebe7 objc_exception_throw + 48
    2   Foundation                          0x00007fff207501c9 -[NSMutableDictionary(NSMutableDictionary) classForCoder] + 0
    3   UIKitCore                           0x00007fff248310ed UIViewControllerMissingInitialTraitCollection + 188
    4   UIKitCore                           0x00007fff24835616 -[UIViewController traitCollection] + 155
    5   UIKitCore                           0x00007fff24824392 -[UITableViewController dealloc] + 196
    6   App Dev                             0x000000010e4f5680 $s05AppLaB027MyViewController
    C14paymentMethods13selectedIndex5offer8delegateACSaySo16AppPaymentMethodCG_SiAA5Offer_pAA08CheckoutcD8Delegate_pSgtcfc + 368
    7   App Dev                             0x000000010e4f5726 $s05AppLaB027MyViewController
    C14paymentMethods13selectedIndex5offer8delegateACSaySo16AppPaymentMethodCG_SiAA5Offer_pAA08CheckoutcD8Delegate_pSgtcfcTo + 102
    8   App Dev                             0x000000010e289291 -[CartViewController showPaymentMethodPickerWithPaymentMethods:] + 385
    9   App Dev                             0x000000010e289055 __59-[CartViewController showPaymentMethodPickerOrEntryForm]_block_invoke + 245
    10  App Dev                             0x000000010e7075ae $sSo16AppServiceResultVSo7NSArrayCSgSo7NSErrorCSgIeyByyy_ABSaySo16AppPaymentMethodCGSgs5Error_pSgIegygg_TR + 222
    11  App Dev                             0x000000010e707246 $sSo17AppAccountServiceC05AppLaD0E19fetchPaymentMethods11forListType7refresh09preferredF6Method10completionSo17AppRequestReceipt_pSgAC0fmiJ0O_S2bySo16AppServiceResultV_SaySo010AppPaymentM0CGSgs5Error_pSgtcSgtFyAN_ArTtcfU_ + 630
    12  App Dev                             0x000000010e707333 $sSo17AppAccountServiceC05AppLaD0E19fetchPaymentMethods11forListType7refresh09preferredF6Method10completionSo17AppRequestReceipt_pSgAC0fmiJ0O_S2bySo16AppServiceResultV_SaySo010AppPaymentM0CGSgs5Error_pSgtcSgtFyAN_ArTtcfU_TA + 35
    13  App Dev                             0x000000010e580474 $sSo16AppServiceResultVSaySo16AppPaymentMethodCGSgs5Error_pSgIegygg_ABSo7NSArrayCSgSo7NSErrorCSgIeyByyy_TR + 212
    14  App Dev                             0x000000010e39bcad __79-[AppAccountV3DAO fetchPaymentMethodsRefresh:preferredPaymentMethod:withBlock:]_block_invoke + 45
    15  libdispatch.dylib                   0x0000000110c18a18 _dispatch_call_block_and_release + 12
    16  libdispatch.dylib                   0x0000000110c19bfc _dispatch_client_callout + 8
    17  libdispatch.dylib                   0x0000000110c28366 _dispatch_main_queue_callback_4CF + 1195
    18  CoreFoundation                      0x00007fff2036a555 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    19  CoreFoundation                      0x00007fff20364db2 __CFRunLoopRun + 2772
    20  CoreFoundation                      0x00007fff20363dfb CFRunLoopRunSpecific + 567
    21  GraphicsServices                    0x00007fff2cbb5cd3 GSEventRunModal + 139
    22  UIKitCore                           0x00007fff24fee193 -[UIApplication _run] + 928
    23  UIKitCore                           0x00007fff24ff2bfb UIApplicationMain + 101
    24  App Dev                             0x000000010e267120 main + 96
    25  dyld                                0x0000000110654e1e start_sim + 10
    26  ???                                 0x0000000000000001 0x0 + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)

最佳答案

注释掉很多代码后,我开始收到一个新错误:
fatal error :对类“PaymentPickerViewController”使用未实现的初始化程序“init(style:)”
再研究一下行为,似乎我们有一个 objc 初始化扩展。我相信在这个 Xcode 13 中,objc 代码被破坏并且没有“看到”对初始化程序的根调用。
我们原来的初始化代码路径是这样的:

@objc extension PaymentPickerViewController {
    convenience init() {
        dataProvider = DataProvider()
        self.init(dataProvider: dataProvider)
    }
}

init(dataProvider: DataProvider) {
   self.dataProvider = dataProvider
   super.init(style: .plain)        
}
该修复明确覆盖了我们想要从 super 中使用的 swift init:
override init(style: UITableView.Style) {
    super.init(style: .plain)
     self.dataProvider = DataProvider()
}
这会强制 objc 看到正确的 init,然后它就可以工作了。
我担心我发布的原始错误非常具有误导性,因此希望这可以帮助遇到此问题的任何人。

关于swift - Xcode 13 beta 5 错误 : UIViewController is missing its initial trait collection populated during initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69092599/

相关文章:

swift - 用泛型基类的泛型覆盖属性

objective-c - 是否可以从 Objective-C 中的预处理器 DEFINE 获取本地 IP 地址?

ios - 为什么当内容大小类别发生变化时,不为表格 View 单元格调用 traitCollectionDidChange?

ios8 - 当另一个 View Controller 显示在它上面时,UIPresentationController 会改变大小

ios - 初始化程序和函数中的参数之间的 Swift 语法差异

ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures

ios - 命令/usr/bin/codesign 失败,退出代码为 1 Xcode CocoaPods

ios - 如何从嵌套的 subview Controller 中检查屏幕的当前尺寸等级?

swift - 变量在闭包之外不会改变

ios - 是什么导致了我的 EXC_BAD_ACCESS? (iPhone 开发工具包)