【问题标题】:Swift: CoreData - unrecognized selector sent to instance when insert rowSwift:CoreData - 插入行时发送到实例的无法识别的选择器
【发布时间】:2015-11-02 16:55:26
【问题描述】:

这个错误很奇怪:

-[<>.Tips setTipName:]: unrecognized selector sent to instance <>

名称 setTipName 不会出现在代码中的任何地方,但有一个变量tipName(注意小写“t”

我正在尝试在 CoreData 实体中插入一行

class Tips: NSManagedObject {
@NSManaged var sectionNumber: NSNumber
@NSManaged var tipDescription: String
@NSManaged var viewName: String
@NSManaged var tipName: String
@NSManaged var tipOrder: NSNumber
@NSManaged var tipType: String
@NSManaged var tipLinkName: String
}

这是插入的代码:

func createNewTips ()
{
    //  set create all switches and set to off
    var error: NSError? = nil
    var textCount = textArray.count
    for var i = 0; i<textCount; ++i
     {
        var newTip = NSEntityDescription.insertNewObjectForEntityForName("Tips", inManagedObjectContext: managedObjectContext!) as! Tips
        newTip.viewName    = viewName
        newTip.tipName = textArray [i].tipName
      NSLog("after tipName")
        newTip.tipDescription = textArray[i].tipDescription
        NSLog("after set tipDescription")
        newTip.tipOrder = textArray[i].tipOrder
        NSLog("after set tipOrder")
        newTip.sectionNumber = textArray[i].sectionNumber
        NSLog("after set sectionNumber")
        newTip.tipType = textArray[i].type
        NSLog("after set type")
        newTip.tipLinkName = textArray[i].tipLinkName
        var error: NSError? = nil
        if !managedObjectContext!.save(&error) {
            NSLog("error &d", error!)
            abort()
        } // end save

    } // end loop

} // end of createNewSwitches

我已经多次重新创建数据模型

我还更改了属性的顺序,错误发生在不同的属性上。我注意到,当我将 viewName 属性移到列表后面时,它似乎是第一个属性。

这是 textArray 中的代码

   var textArray:[(sectionNumber: Int, tipOrder: Int, tipName: String, tipDescription: String, type: String, tipLinkName:String)] =
[
    (1,0,"sw1","Check in and around your home for damage","text",""),
    (1,1,"sw2","Dispose of any spoiled or contaminated foods, especially after a power outage. If you’re not sure, throw it out. You can check the food safety tips below","text",""),
    (1,2,"sw3","Encourage family members to talk about their experience and their feelings, especially children","text",""),
    (1,3,"sw4","Contact other family members to let them know that you are safe","text",""),
    (2,0,"sw5","Check Utilities","link",""),
    (3,0,"sw6","Food Safety Tips","link",""),
]

对此有什么建议吗?

【问题讨论】:

  • 错误信息看起来不完整——尖括号之间应该有一些东西。
  • 模型构建器中提示实体的类是否设置为“提示”?
  • 当您使用propertyName = someValue; 设置属性时,您正在调用setPropertyName 方法。
  • 尖括号是我删除不相关数据的方式
  • @PatriciaW- 如果你不理解错误信息,你就不知道什么是相关的。

标签: swift core-data


【解决方案1】:

setTipName 方法是在后台为 NSManagedObject 子类创建的自动生成的 setter 方法。即使您使用建模器创建 NSManagedObject 子类,它也不会出现在代码中。

Core Data 必须将所有建模属性包装在 getter 和 setter 中,以确保触发键值观察、验证等。命名是自动的,并遵循旧的 Objective-C 约定。还有tipNamegetTipName 方法。

我怀疑你实际上并没有从插入中得到一个 Tip 对象。 我在 Swift 上落后,但我擅长使用 Core Data,我不认为“as!”这里应该需要演员表。

var newTip = NSEntityDescription.insertNewObjectForEntityForName("Tips", inManagedObjectContext: managedObjectContext!) as! Tips

... 因为编译器应该期待一个 Tips 对象。错误消息中的空"&lt;&gt;" 表明您实际上没有Tips 对象(或者您是否编辑了错误消息。)

如果这个 Objective-C 错误的答案肯定是你从插入返回了错误的类。最常见的错误原因是:

  1. 未能在 Core Data 模型编辑器中分配 NSManagedObject 子类名称,并将其保留为通用 NSManageObject
  2. 模型中的类名拼写错误,例如“小费”、“小费”、“小费”等。

【讨论】:

  • 需要as!演员,否则newTip将具有NSManagedObject类型而不是Tips
猜你喜欢
  • 2020-12-25
  • 1970-01-01
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多