ios - -[__NSArrayM objectAtIndex :]: index 9223372036854775807 beyond bounds [0 . 。 8] swift

标签 ios objective-c swift

我正在创建一个带有用于导航的 UITabBar 的应用程序,为了拥有比 Apple 提供的标准 5 个选项卡更多的功能,我使用了一个名为 JFATabBarController 的框架。 . 该框架是用 Objective-C 编写的,而我的应用程序的其余部分是用 Swift 编写的。在其中一个选项卡中,我有一个按钮列表,当用户按下按钮时,它开始使用 AVPlayerViewController 播放视频。视频在纵向模式下播放正常,但一旦手机转到横向模式,我的应用程序就会崩溃,并在控制台中显示以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 8]'
*** First throw call stack:
(0x181c56e38 0x1812bbf80 0x181b36ebc 0x100157efc 0x186daf4b4 0x18714d0f0 0x18714a3b4 0x186ea462c 0x186ea4494 0x18740c32c 0x1870ba244 0x18740c124
0x186dd8e90 0x186df09c8 0x18740bfe4 0x18709bf30 0x18709d190 0x186e24b6c 0x186e244a0 0x186e1ac20 0x186da21c8 0x181bf8eac 0x181bf86cc 0x181bf844c
0x181c61494 0x181b36788 0x18253e89c 0x186da183c 0x187060cf0 0x187060c60 0x186da164c 0x186da0eb8 0x186e106b0 0x18341d86c 0x18341d358 0x181c0d85c
0x181c0cf94 0x181c0acec 0x181b34d10 0x18341c088 0x186e09f70 0x10007c138 0x1816d28b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

应用程序是纵向的,所以它甚至不应该变成横向。

在 JFATabBarController.m 的第 119 行抛出错误 Thread 1: signal SIGABRT:

UIButton *oldButton = self.tabButtons[self.selectedIndex];

tabButtons 是在 JFATabBarController.m 的第 73 行定义的 NSMutableArray:

- (NSMutableArray *)tabButtons
{
    if (!_tabButtons)
    {
        _tabButtons = [NSMutableArray new];
    }
    return _tabButtons;
}

self.selectedIndex与当前选中的选项卡项关联的 View Controller 的索引。如 Apple 文档中所述。

现在,我做了一些研究,发现 9223372036854775807 是最大可能的变量,也是 NSNotFound 的常量。我还读到我需要使用 if 语句检查 NSNotFound,但是当我尝试这样做时,应用程序仍然崩溃并将错误移至 if 语句。

所以目前我有点没有想法,我希望这里有一些熟练的人可以帮助我!谢谢!

最佳答案

正如您所指出的,self.selectedIndex 等于 NSNotFound
您可以按如下方式更新代码:

if (self.selectedIndex != NSNotFound) {
    UIButton *oldButton = self.tabButtons[self.selectedIndex];
    ...
}

如果 self.selectedIndex 可以设置为其他无效值,您可以按如下方式更新条件(以确保它在范围内):

if (self.selectedIndex >= 0 && self.selectedIndex < [self.tabButtons count]) ...

*self.selectedIndex >= 0 仅在 selectedIndex 未签名时才需要。

关于ios - -[__NSArrayM objectAtIndex :]: index 9223372036854775807 beyond bounds [0 . 。 8] swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353132/

相关文章:

ios - 创建一个空的 var 给我一个错误

objective-c - 在 UIPopoverController 中添加一个 View

ios - 如何使用代码为 UIButton 创建自定义图像?

ios - 使用 fbsdk 4.2 获取 Facebook 用户数据

xcode - 通过 UIActivityViewController Swift 问题分享链接和图像

swift GCDAsyncSocket acceptOnPort :Error: not opening a port?

ios - 在 Objective C 中添加二维数组的对角线

ios - 如何将 UIImagePickerController 与 UINavigationController 结合使用

ios - 添加动画时,UITableView 停止工作

android - 我可以使用后台任务每 1 秒处理一次来自服务器的数据吗?