我是swift的新成員,並且核心數據出現問題。Swift核心數據嘗試填充時出錯
嘗試填充時,我的應用崩潰。我只是有1場是用於測試目的
import UIKit
import SwiftyJSON
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
seedRestaurantList()
fetch()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fetch() {
let moc = DataController().managedObjectContext
let nameFetch = NSFetchRequest(entityName: "RestaurantList")
do {
let fetchedName = try moc.executeFetchRequest(nameFetch) as! [RestaurantList]
print(fetchedName.first!.name!)
} catch {
fatalError("merda again: \(error)")
}
}
func seedRestaurantList() {
let moc = DataController().managedObjectContext
let entity = NSEntityDescription.insertNewObjectForEntityForName("RestaurantList", inManagedObjectContext: moc) as! RestaurantList
entity.setValue("teste", forKey: "name")
do {
try moc.save()
} catch {
fatalError("deu merda: \(error)")
}
}
}
這是我得到提前
--- RestaurantList + CoreDataProperties.swift
2015-11-02 17:38:04.880 DinDins[2993:1252158] CoreData: error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.". No last error recorded. 2015-11-02 17:38:04.885 DinDins[2993:1252158] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.' * First throw call stack: (0x236c585b 0x3503adff 0x2346da89 0x2346dc95 0x2346de2d 0x2347710f 0x1249d03 0x12534fb 0x23468ef1 0x233b31f3 0x233b3223 0x233d615b 0x9cff0 0x9c360 0x9c400 0x277f9f55 0x278b6c4f 0x278b6b45 0x278b5ef1 0x278b5b27 0x278b577d 0x278b56f7 0x277f5cc3 0x270bdb05 0x270b9201 0x270b9091 0x270b85b1 0x270b8263 0x270b1a1f 0x23688091 0x23686387 0x235d90f9 0x235d8ecd 0x27867607 0x278622dd 0x9fde0 0x35788873) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
由於錯誤
import Foundation
import CoreData
extension RestaurantList {
@NSManaged var name: String?
}
--- RestaurantList.swift
import Foundation
import CoreData
class RestaurantList: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
您是否修改了現有的核心數據結構? –
剛剛添加了RestaurantList + CoreDataProperties.swift和RestaurantList.swift –
而且您沒有爲您的MOC添加任何新參數? –