【问题标题】:Why do I get NS range exception crash on iPad?为什么我在 iPad 上会出现 NS 范围异常崩溃?
【发布时间】:2021-04-01 17:03:41
【问题描述】:

我在 Firebase 上收到了一定数量的崩溃报告,所有这些都发生在 iPad 上。

Fatal Exception: NSRangeException
*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
_CFDataInit


Fatal Exception: NSRangeException
0  CoreFoundation                 0x1ae9139d8 __exceptionPreprocess
1  libobjc.A.dylib                0x1c2c7cb54 objc_exception_throw
2  CoreFoundation                 0x1ae97dd98 -[__NSCFString characterAtIndex:].cold.1
3  CoreFoundation                 0x1ae7fcb84 _CFDataInit
4  UIKitCore                      0x1b09d6630 -[UITabBarController _viewControllerForTabBarItem:]
5  UIKitCore                      0x1b09d6690 -[UITabBarController _tabBarItemClicked:]

有没有办法找出并解决这个问题?

【问题讨论】:

    标签: ios swift uitabbarcontroller


    【解决方案1】:

    简短的回答是:Apple 在某些情况下会添加一个额外的标签栏项目。该额外的标签栏项目位于标签栏右侧的某处。它是不可见的,但如果您或您的用户不小心点击了它,则会触发超出范围的错误。

    一个简单的解决方案是在适当的时候运行以下代码。

            // If you started your app project with Apple's Tabbed App template, that is your root view controller. 
            guard let rootViewController = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController as? UITabBarController,
                  let items = rootViewController.tabBar.items else {return}
            for item in items {
                //Find the tab bar item that has no title, which means it is added by Appple automatically
                guard item.title == nil else {continue}
                //Disable it so that it won't trigger a _tabBarItemClicked event
                item.isEnabled = false
            } 
    

    我如何知道为什么会发生这种崩溃?

    明显的提示是崩溃报告中的 _tabBarItemClicked。它导致索引为 5。但我的应用程序在主情节提要中仅绘制了五个标签栏项目。然后我使用 XCode 的视图调试器来检查视图层次结构。它确实有 6 个酒吧项目。

    我做了一些额外的挖掘,并弄清楚了为什么要添加额外的酒吧项目。这是因为我有一个启动广告,它在应用启动时覆盖了应用的完整视图。当我创建该视图时,Apple 认为我的标签视图栏有超过 5 个项目并自动添加了一个“更多”项目。

    我为什么不改变启动广告的展示方式?

    我想如果我使用视图控制器而不是选项卡视图控制器作为根视图,Apple 可能不会添加那个额外的栏项目。但是在应用程序的整个生命周期中,我必须在视图控制器中嵌入一个选项卡视图控制器。我认为当前的解决方案更加安全和高效。当然,在未来,切换到 Swift UI 和协议会避免很多这样的潜在问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多