2015-11-02 51 views
2

我是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 


} 
+0

您是否修改了现有的核心数据结构? –

+0

刚刚添加了RestaurantList + CoreDataProperties.swift和RestaurantList.swift –

+0

而且您没有为您的MOC添加任何新参数? –

回答

0

里卡多,尝试重置您的模拟器。打开模拟器,转到菜单模拟器并选择重置内容和设置

+0

我正在iPhone上测试。我应该避免这样做吗? –

+0

删除你的应用程序并再次安装 –

+0

我已经删除了该项目并创建了一个从头开始,它的工作,谢谢 –