There are three steps to adding Core Data to an existing project. CRUD with Core Data in 8 Steps (Swift 4) - Duration: 29:54. Programming With Swift 829 views. This implementation creates and return a coordinator, having added the store for the application to it. A Simple Demo App Looking at the other app templates included in Xcode 7, it seems that only the Master-Detail Application and Single View Application have the option to include Core Data. = nil            if moc.hasChanges && ! {                // Replace this implementation with code to handle the error appropriately. You need to copy your data model file from the other project you created to your main project. So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we’ll be deleting it when we’re done. Emphasis on solid theoretical background, design principles, app architecture, best coding practices, monthly new videos and updates. = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(projectName + “.sqlite”)        var error: NSError? 2. Step 3: Name the file and click "Save". In the code you just added in the previous step, update the projectName variable with the name of your project, and the dataModelName variable with the name you used for the Data Model you created in step 1. You can name it whatever you want. You should not use this function in a shipping application, although it may be useful during development. There are three steps to adding Core Data to an existing project. var dict = [String: AnyObject]()            dict[NSLocalizedDescriptionKey] = “Failed to initialize the application’s saved data”            dict[NSLocalizedFailureReasonErrorKey] = failureReason            dict[NSUnderlyingErrorKey] = error            error = NSError(domain: “YOUR_ERROR_DOMAIN”, code: 9999, userInfo: dict)            // Replace this with code to handle the error appropriately. Learn the basics of using Core Data in this beginning series! The purpose of this article is simply to step the reader through adding the necessary piece to get Core Data into a previously created project; I defer to those other writings to describe the best techniques for Core Data use. NSLog(“Unresolved error \(error), \(error!.userInfo)”)            abort()        }, lazy var managedObjectContext: NSManagedObjectContext? To use a data base we need to have a ‘xcdatamodeld’ file in the project. return NSManagedObjectModel(contentsOfURL: modelURL)! You can generate one by going to File > New … If the template you want to use doesn’t support Core Data, add Core Data to the project as described in Setting Up a Core Data Stack. = {        // The persistent store coordinator for the application. Adding Core Data to an existing Swift project. In most cases, you'll be adding Core Data to an existing app. To kick things off, download the starter project for this tutorial using the Download Materials button at the top or bottom of this page. Don’t worry, the steps below show just how easy it is to add Core Data to your project. That provides for us the complete ability to use Core Data fetch requests from within SwiftUI, although we still need to add some example data to work with. To use a data base we need to have a ‘xcdatamodeld’ file in the project. Core Data creates its own Movie class so you need to remove Movie.swift.Delete Movie.swift by right-clicking it in the Project navigator and selecting Delete.In the resulting dialog, click Move to Trash.. A data model is a description of the data you want Core Data to store, and is a bit like creating a class in Swift: you define entities (like classes) and give them attributes (like properties). Open Movie.swift.At the beginning of this tutorial, Movie struct was the model object. If you prefer Objective-C, then I recommend reading my earlier series on the Core Data framework. Add a “Data Model” to your project: File -> New -> File … iOS -> Core Data -> Data Model -> Next. Then add Core Data with CloudKit as described in Update an Existing Xcode Project. Enable iCloud. In the applicationWillTerminate function, add the following code: // Saves changes in the application’s managed object context before the application terminates. Once you have added you data model file you need to update your AppDelegate file. You’ll see the “Use Core Data” checkbox on the … I suggest using this for consistency, and because it slightly simplifies a later step. Add a Core Data Model to an Existing Project Choose File > New > File and select from the iOS templates. You also need to add the following method call to the applicationWillTerminate function. = {        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) Core Data is a really handy library for introducing persistence to a Xcode project. The only thing you have to do is replace the database name from your demo project with the name you chose for the Core Data Model you created. Smile! 1. = nil        var failureReason = “There was an error creating or loading the application’s saved data.”        if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {            coordinator = nil            // Report any error we got.