Reputation: 154
I am creating an application with a UITabBar
for navigation, and in order to have more then the standard 5 tabs Apple gives you I use a framework called JFATabBarController.
The framework is written in Objective-C, while the rest of my app is written in Swift. In one of the tabs I have a list of buttons and when the user presses a button it starts to play a video using AVPlayerViewController
. The video plays fine in portrait mode but as soon as the phone is turned to landscape my app crashes and gives me the following error in the console:
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
The app is in portrait so it shouldn't even be possible to turn to landscape.
The error Thread 1: signal SIGABRT
is thrown at line 119 of JFATabBarController.m:
UIButton *oldButton = self.tabButtons[self.selectedIndex];
tabButtons
is an NSMutableArray
defined on line 73 of JFATabBarController.m:
- (NSMutableArray *)tabButtons
{
if (!_tabButtons)
{
_tabButtons = [NSMutableArray new];
}
return _tabButtons;
}
And self.selectedIndex
is the index of the view controller associated with the currently selected tab item. as it states in the Apple docs.
Now, I have done some research and found out that 9223372036854775807 is the largest possible variable and also the constant of NSNotFound
. I have also read that I need to check for NSNotFound
with an if
statement, however when I try that, the app still crashes and moves the error to the if
statement.
So currently I am a little out of ideas and I hope that there a some skillfull people here that can help me! Thanks!
Upvotes: 2
Views: 1243
Reputation: 5454
As you pointed out, self.selectedIndex
is equal to NSNotFound
.
You could update your code as follows:
if (self.selectedIndex != NSNotFound) {
UIButton *oldButton = self.tabButtons[self.selectedIndex];
...
}
In case self.selectedIndex can be set to other invalid values, you could update the condition (to make sure it's within bounds) as follows:
if (self.selectedIndex >= 0 && self.selectedIndex < [self.tabButtons count]) ...
*self.selectedIndex >= 0
is only needed if selectedIndex
is not unsigned.
Upvotes: 5