将NSEntity添加到NSOrderedSet时出错

时间:2015-12-08 08:58:26

标签: ios core-data nsmanagedobject swift2.1

我的CoreData结构中有2个实体,它们之间存在一对多的关系。在主实体中我有一个NSOrderdSet,每次我创建一个新的保存时我想在NSOrderdSet中添加并保存第二个实体,但是我得到一个错误。  这是我的代码:

//main Entity 
class Project : NSManagedObject {
func requestAddNewWeek (value: WeekContent) {
let mutableCopyOfSet = projectContent?.mutableCopy() as! NSMutableOrderdSet
mutableCopyOfSet.addObject(value)
projectContent = mutableCopyOfSet.copy() as ? NSOderedSet
}
}

// ViewController where the save occurs
class SetupWeekViewController {
var project : Project?

@IBAction func saveButton (sender : AnyObject) {
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext
 let entity = NSEntityDescription.entityForName("Content", inManagedObjectContext:managedObjectContext
//Here is where the error happens
project!requestAddNewWeek(newWeek)

managedContext.save()
}

//这是我得到的错误

2015-12-08 11:07:13.392 MySightwords[1998:189666] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/Len/Library/Developer/CoreSimulator/Devices/0215DFE7-14EA-459F-991C-5ABF8020712D/data/Containers/Data/Application/7A509174-D568-4CCE-8A3D-9710FF80A9BF/Documents/SingleViewCoreData.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
    metadata =     {
        NSPersistenceFrameworkVersion = 640;
        NSStoreModelVersionHashes =         {
            Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
            Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
            Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
2015-12-08 11:07:13.396 MySightwords[1998:189666] Unresolved error Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7ff7025e9200 {Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}}}, [NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}]
(lldb) 

1 个答案:

答案 0 :(得分:0)

由于您的模型发生了变化。

从模拟器中删除应用程序并对项目执行清理。

注意:这仅用于开发。对于生产,您需要实现某种迁移。谷歌“核心数据迁移”,轻量级迁移是最简单的。