0

I have a UITabController with two UIViewControllers embedded in it. Both of them have the following code in viewWillAppear:

override func viewWillAppear(_ animated: Bool){
    super.viewWillAppear(animated)
    self.navigationController?.isNavigationBarHidden = true

now, sometimes when I really quickly change the views by going back and forth between tabs, I crash the app with the following error:

2017-04-22 22:04:52.033830 appName[433:61198] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x1934e91b8 0x191f2055c 0x1933c471c 0x100111d90 0x100117870
0x1996e1b58 0x1996e1d88 0x1996cf320 0x1996e6dec 0x199482de8 
0x19939ba80 0x1968499d8 0x19683e4cc 0x1993b0500 0x199458720
0x199456004 0x19950fa20 0x19950f580 0x100272764 0x100277628
0x1993b6754 0x1993b64cc 0x1994532d0 0x1993b6754 0x1993b64cc
0x1994d8084 0x1994d7d08 0x1994d7b64 0x1994da80c 0x199597544
0x1993d3d30 0x19959734c 0x1993d3d30 0x1993d3cb0 0x1993be128
0x1998ac0bc 0x1993d3d30 0x1993d3cb0 0x1993be128 0x1993d359c
0x19995e628 0x19995a6c0 0x1934960c0 0x193493cf0 0x193494180
0x1933c22b8 0x194e76198 0x1994097fc 0x199404534 0x1001e04e0 
0x1923a55b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I also see the following error:

enter image description here

Do you know what could be the issue here and how could I prevent it?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • It might be too fast. As stupid as this seems to bee. :-) I had always problems with NSRange.. when the device couldn't handle my code. Just like my calculator can't calculate with too big numbers. – mcd Apr 22 '17 at 20:16
  • @maximilian_cs is there a way of preventing the crash? It happene randomly, sometimes even during first switch, it crashes the app and it's pretty annoying :( – user3766930 Apr 22 '17 at 20:20
  • I don't know. I am sorry. – mcd Apr 22 '17 at 20:21
  • They're actually embedded in to the navigation controller? – Mannopson Apr 22 '17 at 21:00
  • Yes, sorry for not mentioning it! Both uiviewcontrollers re embedded, so from each of them user can open other view controllers – user3766930 Apr 22 '17 at 21:18
  • Disable from storyboard instead. – Mannopson Apr 22 '17 at 21:27
  • I was not able to replicate the issue on a plain project with a UITabBarController and two View Controllers: https://github.com/rikoschmidt/UITabBarControllerCrashDemo. Are you sure it is not caused by some other operation you perform in your view controllers? – riik Apr 22 '17 at 21:35
  • The exception says `index 0 beyond bounds for empty array`, do you have any arrays? – Toma Apr 22 '17 at 21:59
  • I think this is the point in code that causes this exception - I wrote `print` messages directly before and after it and during the crash I see - as a last print - the message right before that line. Also, I was moving this line back and forth in `viewWillAppear` and it always crashed there... – user3766930 Apr 22 '17 at 22:45

2 Answers2

0

I was unable to replicate the crash, seems like it might be something else? Also, try implementing the code with it in the viewDidLoad() which is generally more suited for UI stuffs in the app lifecycle.

override func viewDidLoad(){
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = true
}
PaulBart1
  • 61
  • 4
  • Thanks for the hint, however I cannot put it in `viedDidLoad`...it is embedded in navigation controller and other panels(that are accessible from the view without navigation bar) have the navigation bar visible...So every time user comes back from those panels to the parent UIViewController,I have to hide the navigation bar. That's why `viewWillAppear` seems like a good place for me to have this code...Also, I'm pretty sure the crash happens here, I put `prints` before and after it and moved it up and down to different lines in `viewWillAppear` - every time the last `print` is the one before:( – user3766930 Apr 22 '17 at 22:19
0

It was hanging on ios 11.1.1. changing to the twin setter

navigationController!.setNavigationBarHidden(true, animated: false)

worked around this jawpop inspiring android-grade bug

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66