Although I know that some hardcore OOP proponents will be unhappy with this decision, I suggest using singleton classes to manage master data in the application.
I would advise creating a global CoreDataManager that can be accessed through a shared instance. Now you have global access to your search, update, delete, etc. methods, and your global variable remains confidential.
private var sharedCoreDataManager: CoreDataManager! class CoreDataManager { let managedContext: NSManagedObjectContext class var shared: CoreDataManager { return sharedCoreDataManager } class func initialize(context: NSManagedObjectContext) { sharedCoreDataManager = CoreDataManager(context: context) } private init(context: NSManagedObjectContext) { managedContext = context } func delete(entity: String, index: Int) -> Bool { var data = fetch(entity) if data != nil { managedContext.deleteObject(data![index]) data!.removeAtIndex(index) managedContext.save(nil) return true } return false } func fetch(entity: String) -> [NSManagedObject]? { var request = NSFetchRequest(entityName: entity) var error: NSError? if let entities = managedContext.executeFetchRequest(request, error: &error) as? [NSManagedObject] { if entities.count > 0 { return entities } } return nil } func save(entity: String, _ attributes: [String: AnyObject]) -> NSManagedObject? { var entity = NSEntityDescription.entityForName(entity, inManagedObjectContext: managedContext) let object = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.managedContext) for (key, attr) in attributes { object.setValue(attr, forKey: key) } var error: NSError? if !managedContext.save(&error) { return nil } return object } }
This can be initialized inside your function AppDelegate didFinishingLaunchingWithOptions
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { CoreDataManager.initialize(self.managedObjectContext!) return true }
You can configure NSManagedObject by clicking YourProject.xcdatamodeld in the navigator. In your case, you add a Job object with the attributes salary (double) and date (date). From the main menu, choose Editor> CreateNSManagedObjectSubclass to automatically generate a subclass of Work. While you are still in the xcdatmodel editor, open the right-most panel - you should see text fields for "Name" and "Class". Be sure to change your class to "ProjectName.Name" - in your case, "ProjectName.Job" - or you will not be able to instantiate the new NSManagedObject class.
Your NSManagedObject class should be automatically created for you and available for verification in the project navigator. It will look like this:
import Foundation import CoreData @objc class Job: NSManagedObject { @NSManaged var salary: NSNumber @NSManaged var date: NSDate }
To limit access to your managed objects, you must create intermediary classes with the get- and set-style variables. Although Swift does not have a βsecureβ access level, you can keep your NSManagedObjects private and allow access through object intermediaries by grouping them into a single class file:
class ManagedObjectMediator<T: NSManagedObject> { private var managedObject: T! init?(_ type: String, attributes: [String: AnyObject]) { if let newManagedObject = CoreDataManager.shared.save(type, attributes) { managedObject = newManagedObject as T } else { return nil } } } class JobMediator<T: Job>: ManagedObjectMediator<Job> { var date: NSDate { return managedObject.date } var salary: NSNumber { return managedObject.salary } init?(attributes: [String:AnyObject]) { super.init("Job", attributes: attributes) } }