0
votes

This error is strange:

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

the name setTipName does not occur anywhere in the code but there is a variable tipName (note the lower case "t"

I am attempting to insert a row into a CoreData entity

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
}

Here is the code doing the insert:

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

I've recreated the data model several times

I've also changed the order of the attributes and the error occurs on a different attribute .. I've noticed that it appears to be the first attribute when I move the viewName attribute later in the list.

Here is the code in 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",""),
]

Any suggestions about this?

1
The error message looks incomplete – there should be something between the angle brackets.Martin R
Is the class set to "Tips" for the Tips Entity in the model builder?Aaron Rasmussen
When you set a property with propertyName = someValue;, you are invoking the setPropertyName method.Hot Licks
The angle brackets were my way of removing irrelevant dataPatriciaW
@PatriciaW- If you don't understand the error message, you don't know what is relevant or not.TechZen

1 Answers

2
votes

The method setTipName is an auto-generated setter method created for NSManagedObject subclasses behind the scenes. It won't appear in code even if you use the modeler to create the NSManagedObject Subclass.

Core Data has to wrap all all modeled attributes in getters and setters to ensure that key-value observing, validation etc gets trigger.The naming is automatic and follows the old Objective-C convention. There will also be either a tipName or getTipName method.

I suspect you are not actually getting a Tip object back from the insertion. I'm behind the curve on Swift but I'm good with Core Data, I don't think the "as!" cast should be needed here.

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

… because the compiler should be expecting a Tips object. The empty "<>" in the error message suggest that you don't in fact have a Tips object (or did you edit the error message.)

Were this Objective-C the answer to the error would definitely be that you have the wrong class returned from the insert. The most common causes of the error are:

  1. Failing to assign a NSManagedObject subclass name in the Core Data model editor and leaving it just a generic NSManageObject
  2. Misspelling the class name in the model e.g. "Tip", "tips", Tipps" or some such.