How do I get a reference to the app delegate in Swift?
Ultimately, I want to use the reference to access the managed object context.
How do I get a reference to the app delegate in Swift?
Ultimately, I want to use the reference to access the managed object context.
it is very simple
App delegate instance
you can call a method with one line syntax
you can access a variable with this code
In my case, I was missing
import UIKit
on top of myNSManagedObject
subclass. After importing it, I could remove that error asUIApplication
is the part ofUIKit
Hope it helps others !!!
Here is the Swift 2.0 version:
And to access the managed object context:
or, using guard:
As of iOS 12.2 and Swift 5.0,
AppDelegate
is not a recognized symbol.UIApplicationDelegate
is. Any answers referring toAppDelegate
are therefore no longer correct. The following answer is correct and avoids force-unwrapping, which some developers consider a code smell:I use this in Swift 2.3.
1.in AppDelegate class
2.Call AppDelegate with
In Swift 3.0 you can get the
appdelegate
reference by