gpt4 book ai didi

ios - swift 中自定义 NSManagedObject 类核心数据的问题

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:06 25 4
gpt4 key购买 nike

我最近开始学习swift,我想使用一些用Objective C写的数据模型类。当我试图从输入框保存数据时,我遇到了一个奇怪的错误:

用户.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

用户.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

这是数据模型截图:

data model这是保存功能:

@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无关。如果您更新您的核心数据模型没有定义合并/版本控制规则,如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.

您不能期望更改已安装的模型无需进一步努力即可在设备上运行。

关于ios - swift 中自定义 NSManagedObject 类核心数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com