Swift:CoreData - 在插入行时发送到实例的无法识别的选择器

时间:2015-08-10 17:05:03

标签: swift core-data

这个错误很奇怪:

-[<>.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",""),
]

有关于此的任何建议吗?

2 个答案:

答案 0 :(得分:2)

方法(defun calculate(l) (cond ((eql (cadr l) '+) (+ (car l) (cddr l))) ((eql (cadr l) '*) (- (car l) (cddr l))) ) ) 是在幕后为NSManagedObject子类创建的自动生成的setter方法。即使您使用建模器创建NSManagedObject子类,它也不会出现在代码中。

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

怀疑你实际上并没有从插入中获取Tip对象。 我落后于Swift的曲线但我对Core Data很好,我不认为“as!”这里应该需要演员。

getTipName

...因为编译器应该期待一个var newTip = NSEntityDescription.insertNewObjectForEntityForName("Tips", inManagedObjectContext: managedObjectContext!) as! Tips 对象。错误消息中的空Tips表示您实际上没有"<>"对象(或者您编辑了错误消息。)

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

  1. 未能在Core Data模型编辑器中分配NSManagedObject子类名称,只留下通用NSManageObject
  2. 在模型中拼错了类名,例如“提示”,“提示”,Tipps“或其他一些。

答案 1 :(得分:0)

感谢@RomanSausarnes,我意识到我使用的是错误的数据模型。重建它修复了一切。