我有一个我的应用代表的参考
我该如何解决这个错误? @H_404_4@编辑:
我还需要在这里访问上下文:
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate@H_404_4@这工作正常,但下面这行代码给出了一个错误:
let context: NSManagedObjectContext = appDel.managedObjectContext@H_404_4@它给我以下错误:
'vctGebruikers.Type' does not have a member named 'appDel'@H_404_4@我在他的课堂下面宣布他们是这样的:
class vctGebruikers: UIViewController,UITableViewDelegate,UITableViewDataSource { let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext }@H_404_4@奇怪的是,当我将代码粘贴到viewDidLoad或函数中时,代码才能正常工作.我无法弄清问题是什么.
我该如何解决这个错误? @H_404_4@编辑:
我还需要在这里访问上下文:
let results: NSArray = context.executeFetchRequest(request,error: nil)@H_404_4@这是我在@Antonio工作的原因,但现在我无法访问上下文和appDel
init(coder aDecoder: NSCoder!) { let appDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate self.appDel = appDelegate self.context = appDelegate.managedObjectContext super.init(coder: aDecoder) }
解决方法
在swift中,在初始化所有类属性之前,您无法引用self.
您在此行中隐式使用self:
您在此行中隐式使用self:
let context: NSManagedObjectContext = appDel.managedObjectContext@H_404_4@解决方案是在初始化程序中初始化它们,但使用类似于:
let appDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate self.appDel = appDelegate self.context = appDelegate.managedObjectContext@H_404_4@另外阅读here,几天前在SO上提出了类似的问题.