Christopher
Christopher

Reputation: 45

Swift - How to define core data context in OS X

Thanks to online tutorials I have been using core data with swift and iOS. To define a context I have used

let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

How do I define the context when writing an OS X application?

Upvotes: 1

Views: 1061

Answers (2)

Yelims01
Yelims01

Reputation: 47

You could also have done:

let context = (NSApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
  • UIApplication has changed to NSApplication
  • 'as!' changed to 'as'

Upvotes: 0

Prontto
Prontto

Reputation: 1681

I define it that way:

lazy var context: NSManagedObjectContext? = {
    let appDel = NSApplication.sharedApplication().delegate as! AppDelegate
    if let moc = appDel.managedObjectContext {
        return moc
    } else {
        return nil
    }
}()

Upvotes: 1

Related Questions