我最近开始学习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)
答案 0 :(得分:13)
这与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.
您不能指望更改已安装的型号可以在设备上运行,而无需进一步努力。