Bla*_*ard 1 core-data uiapplicationdelegate ios swift
我想在我的Swift iOS应用程序中使用Core Data,但是如何从任何ViewControllers中调用Core Data的managedObjectContext?
假设我想调用在Objective-C中经常使用的这个方法:
self.managedObjectContext = ((AppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext;
Run Code Online (Sandbox Code Playgroud)
但是,当我在Swift文件中编写以下代码时,它收到了错误:UIApplicationDelegate does not have a member called managedObjectContext.
var appDelegate = UIApplication.sharedApplication().delegate
println(appDelegate.managedObjectContext)
Run Code Online (Sandbox Code Playgroud)
为什么会出错?因为我在创建应用程序时检查了Core Data,所以模板自动在AppDelegate.swift文件中定义了managedObjectContext变量.
那么缺少什么?我使用Xcode 6 beta 5.
您需要将应用程序委托转换为项目的应用程序委托类型.
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context = appDelegate.managedObjectContext
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8111 次 |
| 最近记录: |