1

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?

2 Answers 2

1

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
    }
}()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that helped me tremendously!
0

You could also have done:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.