I have a link to my application delegate
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate
This works fine, but this line of code gives an error:
let context: NSManagedObjectContext = appDel.managedObjectContext
This gives me the following error:
'vctGebruikers.Type' does not have a member named 'appDel'
I declared them right under my class as follows:
class vctGebruikers: UIViewController, UITableViewDelegate, UITableViewDataSource { let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext }
It is strange when I paste the code into viewDidLoad or into a function, the code just works fine. And I canβt understand what the problem is. How can I solve this error?
EDIT: I also need to use the context here:
let results: NSArray = context.executeFetchRequest(request, error: nil)
This is what I got thanks to @Antonio, but now I was not able to access the context and appDel
init(coder aDecoder: NSCoder!) { let appDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate self.appDel = appDelegate self.context = appDelegate.managedObjectContext super.init(coder: aDecoder) }
ios swift
Bas
source share