我有一个Objective C项目,我试图从Swift视图控制器调用AppDelegate。此代码无效:
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
您是否已将brining标题添加到项目中?
最重要的是,如果这样做,请确保将“AppDelegate.h”导入桥接头。
在swift类中,您应该可以使用以下命令调用它:
guard let appDele = UIApplication.sharedApplication().delegate as? AppDelegate else {
fatalError("Unable to reference App Delegate, check settings and riding header")
}
let managedContext = appDele.managedObjectContext