【问题标题】:iOS Core Data : error when assigning a value to an attributeiOS核心数据:将值分配给属性时出错
【发布时间】:2015-05-08 17:23:22
【问题描述】:

这是MyApp.xcdatamodeld,很简单:

_

这是与实体关联的类:

class LogItem: NSManagedObject {

    @NSManaged var title: String
    @NSManaged var itemText: String
    @NSManaged var itemRank: String

}

现在这是导致错误的代码:

let newItem = NSEntityDescription.insertNewObjectForEntityForName("LogItem", inManagedObjectContext: self.managedObjectContext!) as! LogItem
newItem.title = "the title of the item"
newItem.itemText = "je suis chérif"
newItem.itemRank = "2" // this line causes an error !

最后是错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[MyApp.LogItem setItemRank:]: unrecognized selector sent to instance 0x155a8e30'

我做错了什么?

【问题讨论】:

  • 我看不到明显的错误。如果删除那一行,代码是否可以正常运行?您确定在创建 class LogItem 之后没有更改 Core Data 模型(或创建另一个版本)吗?
  • 我在创建类 LogItem 后对其进行了修改。我清理了项目,现在它可以工作了!所以实际上根本没有错误。

标签: ios swift core-data xcode6


【解决方案1】:

当我使用核心数据对象时,我使用以下代码行创建实体。

let logEntity = NSEntityDescription.entityForName("LogItem", inManagedObjectContext: self.managedObjectContext!)
let newItem = LogItem(entity: logEntity!, insertIntoManagedObjectCntext: self.managedObjectContext!)

newItem.title = "the title of the item"
newItem.itemText = "je suis chérif"
newItem.itemRank = "2" 

然后保存上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多