我收到一个错误:
AppDelegate没有成员persistentContainer
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext // Error: value of type 'AppDelegate' has no member 'persistentContainer'
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Run Code Online (Sandbox Code Playgroud)
在AppDelegate.swift文件中,NSPersistentStoreCoordinator被定义为默认值.
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: …Run Code Online (Sandbox Code Playgroud)