swift Xcode 13 beta 5错误:UIViewController在初始化过程中缺少初始trait集合

vc6uscn9  于 2024-01-05  发布在  Swift
关注(0)|答案(3)|浏览(251)

应用程序在Xcode 13 beta 5构建之前一直运行良好。
突然在我们的视图控制器中的这行init代码中得到这个错误:

  1. init(dataProvider: DataProvider) {
  2. self.dataProvider = dataProvider
  3. super.init(style: .plain)
  4. dataProvider.delegate = self
  5. }

字符串
这段代码在Xcode 12中工作得很好,没有任何变化,现在它中断了:
“NSInternalInconsistencyException”,原因:“UIViewController缺少在初始化期间填充的初始trait集合。这是一个严重错误,可能是由于在调用UIViewController初始化器之前访问视图控制器上的属性或方法而导致的。视图控制器:<UITableViewController: 0x7f7fbd291bb0>”
我在谷歌上搜索了一下,只有found 1 obscure thread在这个问题上,但它没有太大的帮助。
有时候设置一个断点并进行单步执行可以让它在不崩溃的情况下工作,所以在引擎盖下似乎有某种布局竞争条件,但我正在努力调试。
任何和所有关于如何调试这是赞赏的建议.现在我无法得到更多的信息,以调试.如果你知道什么可能会导致这一点或在哪里看我会很感激你的输入.
下面是错误堆栈跟踪:

  1. *** First throw call stack:
  2. (
  3. 0 CoreFoundation 0x00007fff203fc8a8 __exceptionPreprocess + 242
  4. 1 libobjc.A.dylib 0x00007fff2019ebe7 objc_exception_throw + 48
  5. 2 Foundation 0x00007fff207501c9 -[NSMutableDictionary(NSMutableDictionary) classForCoder] + 0
  6. 3 UIKitCore 0x00007fff248310ed UIViewControllerMissingInitialTraitCollection + 188
  7. 4 UIKitCore 0x00007fff24835616 -[UIViewController traitCollection] + 155
  8. 5 UIKitCore 0x00007fff24824392 -[UITableViewController dealloc] + 196
  9. 6 App Dev 0x000000010e4f5680 $s05AppLaB027MyViewController
  10. C14paymentMethods13selectedIndex5offer8delegateACSaySo16AppPaymentMethodCG_SiAA5Offer_pAA08CheckoutcD8Delegate_pSgtcfc + 368
  11. 7 App Dev 0x000000010e4f5726 $s05AppLaB027MyViewController
  12. C14paymentMethods13selectedIndex5offer8delegateACSaySo16AppPaymentMethodCG_SiAA5Offer_pAA08CheckoutcD8Delegate_pSgtcfcTo + 102
  13. 8 App Dev 0x000000010e289291 -[CartViewController showPaymentMethodPickerWithPaymentMethods:] + 385
  14. 9 App Dev 0x000000010e289055 __59-[CartViewController showPaymentMethodPickerOrEntryForm]_block_invoke + 245
  15. 10 App Dev 0x000000010e7075ae $sSo16AppServiceResultVSo7NSArrayCSgSo7NSErrorCSgIeyByyy_ABSaySo16AppPaymentMethodCGSgs5Error_pSgIegygg_TR + 222
  16. 11 App Dev 0x000000010e707246 $sSo17AppAccountServiceC05AppLaD0E19fetchPaymentMethods11forListType7refresh09preferredF6Method10completionSo17AppRequestReceipt_pSgAC0fmiJ0O_S2bySo16AppServiceResultV_SaySo010AppPaymentM0CGSgs5Error_pSgtcSgtFyAN_ArTtcfU_ + 630
  17. 12 App Dev 0x000000010e707333 $sSo17AppAccountServiceC05AppLaD0E19fetchPaymentMethods11forListType7refresh09preferredF6Method10completionSo17AppRequestReceipt_pSgAC0fmiJ0O_S2bySo16AppServiceResultV_SaySo010AppPaymentM0CGSgs5Error_pSgtcSgtFyAN_ArTtcfU_TA + 35
  18. 13 App Dev 0x000000010e580474 $sSo16AppServiceResultVSaySo16AppPaymentMethodCGSgs5Error_pSgIegygg_ABSo7NSArrayCSgSo7NSErrorCSgIeyByyy_TR + 212
  19. 14 App Dev 0x000000010e39bcad __79-[AppAccountV3DAO fetchPaymentMethodsRefresh:preferredPaymentMethod:withBlock:]_block_invoke + 45
  20. 15 libdispatch.dylib 0x0000000110c18a18 _dispatch_call_block_and_release + 12
  21. 16 libdispatch.dylib 0x0000000110c19bfc _dispatch_client_callout + 8
  22. 17 libdispatch.dylib 0x0000000110c28366 _dispatch_main_queue_callback_4CF + 1195
  23. 18 CoreFoundation 0x00007fff2036a555 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
  24. 19 CoreFoundation 0x00007fff20364db2 __CFRunLoopRun + 2772
  25. 20 CoreFoundation 0x00007fff20363dfb CFRunLoopRunSpecific + 567
  26. 21 GraphicsServices 0x00007fff2cbb5cd3 GSEventRunModal + 139
  27. 22 UIKitCore 0x00007fff24fee193 -[UIApplication _run] + 928
  28. 23 UIKitCore 0x00007fff24ff2bfb UIApplicationMain + 101
  29. 24 App Dev 0x000000010e267120 main + 96
  30. 25 dyld 0x0000000110654e1e start_sim + 10
  31. 26 ??? 0x0000000000000001 0x0 + 1
  32. 27 ??? 0x0000000000000001 0x0 + 1
  33. )

mcdcgff0

mcdcgff01#

在注解掉大量代码后,我开始得到一个新的错误:
致命错误:对类“PaymentPickerViewController”使用未实现的初始化器“init(style:)”
再深入研究一下这个行为,似乎我们有一个objc初始化的扩展。我相信在这个Xcode 13中,objc代码被破坏了,并且没有“看到”对初始化器的根调用。
我们最初的初始化代码路径是这样的:

  1. @objc extension PaymentPickerViewController {
  2. convenience init() {
  3. dataProvider = DataProvider()
  4. self.init(dataProvider: dataProvider)
  5. }
  6. }
  7. init(dataProvider: DataProvider) {
  8. self.dataProvider = dataProvider
  9. super.init(style: .plain)
  10. }

字符串
修复是显式地覆盖我们想要从super使用的swift init:

  1. override init(style: UITableView.Style) {
  2. super.init(style: .plain)
  3. self.dataProvider = DataProvider()
  4. }


这会强制objc看到正确的init,然后它就可以工作了。
我担心我发布的原始错误是非常误导的,所以希望这能帮助任何遇到这个问题的人。

展开查看全部
qyuhtwio

qyuhtwio2#

* 简短描述:* 对于基于Objective-C的实现,可以将对象方法转换为类方法。

xcode13 GM Seed报告了以下错误。错误日志:

  1. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIViewController is missing
  2. its initial trait collection populated during initialization. This is
  3. a serious bug, likely caused by accessing properties or methods on the
  4. view controller before calling a UIViewController initializer. View
  5. controller: <MyViewController: 0x7fa5ae2356c0>'

字符串

对于以下init method

  1. - (instancetype)initWithModel:(MyModel *)myModel {
  2. MyViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyViewController"];
  3. vc.model = myModel;
  4. return vc;
  5. }

init以以下方式从另一个View Controller调用。

  1. // Before - invoking object method -- Results in the error
  2. MyViewController *viewController = [[MyViewController alloc] initWithModel:testModel];
  3. // After - invoking Class method
  4. MyViewController *viewController = [MyViewController initWithModel:testModel];

为了解决这个问题,我将init方法转换为类方法。

  1. + (instancetype)initWithModel:(MyModel *)myModel {
  2. MyViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyViewController"];
  3. vc.model = myModel;
  4. return vc;
  5. }

展开查看全部
pokxtpni

pokxtpni3#

我也有同样的错误,它发生时,应用程序运行在设备上与IOS >= 17,没有问题的IOS <17
错误代码:

  1. FO_AM_CAI_AddNewBankAccViewController *vc = [FO_AM_CAI_AddNewBankAccViewController alloc];

字符串
代码将修复问题:

  1. FO_AM_CAI_AddNewBankAccViewController *vc = [[FO_AM_CAI_AddNewBankAccViewController alloc] initWithNibName:@"FO_AM_CAI_AddNewBankAccViewController" bundle:[NSBundle mainBundle]];

相关问题