please in the second chapter i can’t manage to pass “managedcontext” from the app delegate
if i use UIapplication.shared in viewdidload it works but if i try using the code below in AppDelegate i receive a nil managedcontext when trying to run my programm
guard let vc =
window?.rootViewController as? ViewController else {
The problem is that after setting the managed object context property, you don’t do anything else. What you need to do is put your ViewController inside a NavigationController and then push the new ViewController. In this case, ViewController would be the rootViewController for the UINavigationController.
but i don’t understand why i need a navigationcontroller, normally i should be able to pass my managedcontext without that just by setting my window and root view controller but i can’t get it to work, is it possible to explain the proper code to share my instance with my view controller ?
hey thanks a lot for your message
i’m not able to make the managedcontext connection between the AppDelegate and the viewcontroller, i encountered the same problem in the dog walk chapter
when i use this code:
guard let navController =
window?.rootViewController as? UINavigationController, let viewController =
navController.topViewController as? ViewController else {return true}
viewController.managedContext = coreDataStack.managedContext
my managedcontext in view controller is still nil(of course if use the uiapllication.shared it works just fine)
by the way every time i add the line var window = UIWindow to the AppDelegate(and that bothers me because it exist in the scene delegate but not in the app delegate and that’s i don’t understand very well)
hope someone can help
thanks a lot in advance
im still seeing “Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value” in " let count = try ! managedContext.count(for: fetch)".
i have tried to either put the code in scene delegate, or embed vc in navigation controller. im a beginner in programming. Could you let me know how to solve this?
it is working now. thanks for your hints and i have figured out the way.
i have decided to delete the scenedelegate and remove the code about it in the AppDelegate