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

前端之家收集整理的这篇文章主要介绍了ios – swift中自定义NSManagedObject类核心数据的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近开始学习 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.

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

猜你在找的iOS相关文章