Group Group Group Group Group Group Group Group Group

MyLocations App Help

I am working on the Saving Locations Chapter of the MyLocations app, and get the error unexpectedly found nil , when I added the code setting the tab bar controller as the root in app Delegate, why is this happening and how can I solve this?

anyone? really need help

Hi @isaacballas2, looks like the tableview is returning nil and crashing because you are using forced unwrapping. One way to check this is by using a guard statement. In regards to a solution I would try changing the tabController constant to,
let tabController = mainStoryBoard.instantiateViewControllerWithIdentifier(“yourStoryboard”)
as! UITabBarController
self.window?.rootViewController = tabController

Best,
Gina

It won’t even compile, it says mainStoryboard is unknown identifier. I was following the direction in iOS Apprentice, was there a bug in the book

@fahim any thoughts?

here is the error in full
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/isaacballas/Library/CloudStorage/iCloud Drive/Desktop/LocationTracker/LocationTracker/AppDelegate.swift, line 30

Without seeing your project, I can’t tell you for sure but it looks as if your root view controller might not be a tab view controller. Please compare your project against the final project for the chapter (specifically the storyboard) and see if both are set up the same way. Hopefully, that lets you figure out what is going on …

When I set up the project I set it up as a single page project, then implemented the tab bar on the storyboard… was that my mistake? can I fix it? how would I make the tab bar controller the root?

My suggestion would be to check in the storyboard and make sure that the arrow indicating rootVC (initial VC) is attached to a Tab Bar VC. It’s likely that you either don’t have the initial VC declared or that it’s pointing to a non-Tab Bar VC. Hence, the crash is happening either because there is no rootViewController or it can’t take the rootViewController and cast it as a UITabBarController.

I just checked, it is connected to tab bar, here is a screenshot, I have no clue why this is happening

Two other suggestions then.

  1. Try to to do a clean of the app: hold down option, go to clean build folder. Then, go to Xcode preferences and clear the derived data. (https://stackoverflow.com/questions/46468220/how-can-i-delete-derived-data-in-xcode-9/46468462)

  2. Replace the one line of code that has three ! and turn it into three separate lines of code. This will at least allow you to figure out which thing is causing Xcode to crash.

Remove:
let tabBarController = window!.rootViewController as ! UITabBarController

replace:
let window = self.window!
let rootVC = window.rootViewController!
let tabBarController = rootVC as ! UITabBarController

any solution to the above question ?