// Don't worry about the @NSManaged thing. // it's just a cue to the compiler that the property // will be managed by Core Date class Note: NSManagedObject { @NSManaged var notebook : Notebook } class Notebook: NSManagedObject { @NSManaged var note : ???? }
import CoreData struct CoreDataStack { // MARK: - Properties private let model : NSManagedObjectModel private let coordinator : MSPersistentStoreCoordinator private let modelURL : NSURL private let dbURL : NSURL let context : NSManagedObjectContext // MARK: - Initializers init?(modelName: String){ guard let modelURL = NSBundle.mainBundle().URLForResource(modelName, withExtension: "momd") else { print("Unable to find \(modelName)in the main bundle") return nil} self.modelURL = model URL // Try to create the model from the URL guard let model = NSManagedObjectModel(contentsOfURL: modelURL) else{ print("unable to create a model from \(modelURL)") } self.model = model coordinator = NSPersistentStoreCoordinator(managedObjectModel: model) context = NSManagedObjectContext(concurrencyType: . MainQueueConcurrencyType) context.persistentStoreCoordinator = coordinator // Add a SQLite store located in the documents folder let fm = NSFileManager.defaultManager() guard let docUrl = fm.URLsForDirectory(.DocumentDirectory, inDomains: UserDomainMask).first else { print("manage to people the document folder") return nil } } }