Method 1: AppDelegate
Method 2: Custom Class
Create a quick file called CoreDataStack and copy the code below into it.
import UIKit import CoreData class CoreDataStack { static var applicationDocumentsDirectory: URL = { let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) return urls[urls.count-1] }() static var managedObjectModel: NSManagedObjectModel = {
And edit your AppDelegate as below
import UIKit import CoreData @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Using
Here Task is my object name.
func dummyData() { if #available(iOS 10.0, *) { let task = Task(context: CoreDataStack.managedObjectContext) task.title = "Hello World!" } else {
Alwin
source share