【发布时间】:2015-05-01 02:48:26
【问题描述】:
这就是我目前的做法,但我想知道这是否是 Apple 建议的方式。我读过一些关于此的辩论。
let appDelegate : AppDelegate = NSApplication.sharedApplication().delegate as AppDelegate
if let moc = appDelegate.managedObjectContext {
// do stuff here
}
所以这只是从AppDelegate 到第一个viewController。从那里我猜想使用segues 是绕过managedObjectContext 的方法?
使用上面的代码很烦人,因为我将它输入到viewController 中需要MOC 的每个方法中。当我有一个带有return 语句的函数并且我所有使用MOC 的代码都在if statement 的主体内时,这更加烦人,因为这会产生错误,指出存在/可能不是return .有没有更好的方法来做到这一点,比如让它更全球化?
编辑:
我的 ViewController.swift 文件有这个标题:
import Cocoa
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
并包含一个表格视图
我的 AppDelegate.swift 文件有:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
【问题讨论】:
标签: macos swift core-data dependency-injection nsmanagedobjectcontext