1

I have the following piece of code

protocol JsonParseDescriptor {
 //some required methods
   func parsePrimitives() {
}

extension JsonParseDescriptor where Self: NSManagedObject {
   func parsePrimitives() {
      self.setValue(1, forKey: "id") //this throws an error in swift stating Ambiguous use of setValueForKey
      self.setValue(1, forKey: "id") //this does not throw any compile-time error
   }

}

Any ideas why setValue:ForKey: is causing this error? Interestingly, setValue:ForKeyPath: isn't causing this issue. I'm just afraid of using the latter incase it causes any side-effects and works other than setValue:ForKey: any ideas?

Salman Hasrat Khan
  • 1,971
  • 1
  • 20
  • 27

1 Answers1

0

This was my code that caused the bug.

func create<U : NSManagedObject>(entityName : String, primaryKey : String, value : AnyObject) -> U {
    let created : U = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: providedContext) as! U
    created.setValue(value, forKey: primaryKey)
    return created
}

This is the code that does not create it:

func create<U : NSManagedObject>(entityName : String, primaryKey : String, value : AnyObject) -> U {
    let created = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: providedContext)
    created.setValue(value, forKey: primaryKey)
    return created as! U
}

My guess is that the compiler (or static analyzer) cannot determine where the dynamic dispatch should be routed. Or maybe it's just a bug.

Either way, I hope this workaround helps.

Holly
  • 5,270
  • 1
  • 24
  • 27