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

 mobiledu2502908907 发布于 2023-01-08 09:03

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

Users.h

#import 
#import 


@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 = ;
    };
    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)

Alexander.. 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.

您不能指望更改已安装的模型将在设备上运行,而无需进一步努力.

1 个回答
  • 这与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.
    

    您不能指望更改已安装的模型将在设备上运行,而无需进一步努力.

    2023-01-08 09:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有