Swift:创建新的NSEntity时属性的值类型不可接受 [英] Swift: Unacceptable type of value for attribute when creating new NSEntity

查看:182
本文介绍了Swift:创建新的NSEntity时属性的值类型不可接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下属性的核心数据实体:

I have a Core Data entity set up with the following attributes:

resellerNo:Int
resellerName:String

我已经按照以下步骤设置了NSManagedObject:

I have setup an NSManagedObject as follows:

class Reseller: NSManagedObject
{
    @NSManaged var resellerNo: Int
    @NSManaged var resellerName: String
}

如果我尝试运行此方法:

If I try to run this method:

func createNewReseller(resellerName: String)
{
    let context = app.managedObjectContext

    let resellerEntity = NSEntityDescription.entityForName("Resellers", inManagedObjectContext: context)
    let newReseller = Reseller(entity: resellerEntity!, insertIntoManagedObjectContext: context)

    newReseller.resellerNo = 12
    newReseller.resellerName = resellerName
    saveDatabase()
    Swift.print ("Reseller \(resellerName) created")
}

然后在尝试分配转售商编号时出现崩溃,并显示一条错误消息:

then it crashes when trying to allocate the resellerNo with an error message:

属性值的不可接受类型:property ="resellerNo"; 所需的类型= NSNumber;给定类型= __NSTaggedDate;值= 2001-01-01 00:00:00 +0000.

Unacceptable type of value for attribute: property = "resellerNo"; desired type = NSNumber; given type = __NSTaggedDate; value = 2001-01-01 00:00:00 +0000.

奇怪的是,如果您使用控制台预先打印newReseller.resellerNo,则它可以正常工作.

Strange thing is, if you use the console to print newReseller.resellerNo just beforehand then it works fine.

以完全相同的方式访问其他实体的其他代码也可以正常工作.

Other code accessing other Entities in exactly the same way work fine.

有什么想法吗?

推荐答案

好吧,最终结果很简单.事实证明,我还没有向实体添加类.

OK it turned out to be fairly simple in the end. It turns out I had not added a class to the Entity.

如果您遇到此问题:

  1. 点击xcdatamodel
  2. 选择实体.
  3. 在实用工具"栏中显示数据模型检查器".
  4. 输入定义的类名(以我的经销商为例)

我还必须将类定义更改为此:

I also had to change my class definition to this:

@objc(Reseller)
class Reseller: NSManagedObject
{
    @NSManaged var resellerNo: Int
    @NSManaged var resellerName: String
}

希望这对某人有帮助.

这篇关于Swift:创建新的NSEntity时属性的值类型不可接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆