我最近开始学习
swift,我想使用一些用Objective C编写的数据模型类.当我尝试从输入框保存数据时,我遇到了一个奇怪的错误:
Users.h
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Users : NSManagedobject @property (nonatomic,retain) Nsstring * password; @property (nonatomic,retain) Nsstring * username; - (Nsstring *)toString; - (void)addFunny:(Nsstring *)prefix; @end
Users.m
#import "Users.h"
@implementation Users
@dynamic password;
@dynamic username;
- (Nsstring *)toString
{
return @"The username is \(username) and password is \(password)";
}
- (void)addFunny:(Nsstring *)prefix
{
self.username = [Nsstring stringWithFormat:@"%@%@",prefix,self.username];
}
@end
这是数据模型截图:
这是保存功能:
@IBAction func btnSave_Clicked(){
println("Save \(txtUsername.text)")
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedobjectContext = appDel.managedobjectContext
let ent = NSEntityDescription.entityForName("Users",inManagedobjectContext: context)
var newUser = Users(entity: ent,insertIntoManagedobjectContext: context)
newUser.username = txtUsername.text
newUser.password = txtPassword.text
context.save(nil)
println(newUser)
}
来自lldb的代码:
Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation Couldn’t be completed. (Cocoa error 134100.)" UserInfo=0xb736c50 {Metadata={
NSPersistenceFrameworkVersion = 508;
NsstoreModelVersionHashes = {
Entity = <b2bc8535 3bcfcdf1 81eecadc d32d8511 cc030525 d4eb7d76 94d11d7c f5853918>;
};
NsstoreModelVersionHashesversion = 3;
NsstoreModelVersionIdentifiers = (
""
);
NsstoreType = sqlite;
NsstoreUUID = "0169C569-2A57-47F1-9EF6-684485CB1135";
"_NSAutoVacuumLevel" = 2;
},reason=The model used to open the store is incompatible with the one used to create the store},(error.userInfo)
解决方法
这与swift无关.如果更新coredata模型而不定义
Applee Doc中提到的合并/版本控制规则,则需要在设备或模拟器上删除并重新安装应用程序.
查看错误消息:
The model used to open the store is incompatible with the one used to create the store}
从模拟器/或设备中删除应用程序并对项目执行清理.这应该清除这些问题.删除应用程序时确保没有在调试器中运行,否则它实际上不会正确删除它.
如果你想确定它消失了(模拟器),请检查此目录
Users/INSERT_YOUR_USER_HERE/Library/Application Support/iPhone Simulator/ for your app's folder,under the version you're running.
您不能指望更改已安装的模型将在设备上运行,而无需进一步努力.