0

My xcode version is 6.3.2. I ref https://stackoverflow.com/a/25856755/685060. I had use lazy but tell me my class does not have this member. Can I use this method? or should I need create a function to return appDelegate and lazy to call it? Thanks your help.

class TableViewController: UITableViewController, DetailViewControllerProtocol {

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    lazy var managedContext = self.appDelegate.managedObjectContext // This line
    ....
Community
  • 1
  • 1
lighter
  • 2,808
  • 3
  • 40
  • 59

1 Answers1

0

I am not sure if I understand your question, but try the code below:

lazy var managedContext:NSManagedObjectContext = { 
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate    
    return appDelegate.managedObjectContext! 
}()

I hope it solve your problem!

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • @lighter Sorry I am not in my mac, do you wanna try the one I just update? – Icaro Jun 11 '15 at 04:05
  • I try and I got a new error `unable to infer closure type in the current context` – lighter Jun 11 '15 at 04:08
  • I fix it, and it my answer, `lazy var managedContext:NSManagedObjectContext = { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate return appDelegate.managedObjectContext! }()` let you to update your answer. – lighter Jun 11 '15 at 05:24
  • @lighter Updated! I was doing it in my windows computer I can't believe I forgot to initialize "()" and to put the type "NSManagedObjectContext". Glad you was able to fix it. Good lucky with your app! – Icaro Jun 11 '15 at 05:27