I have a Core Data base, with Post and Client entities. Post has one-to-many relationships.
I have an error with the lines self.addToClients(newClient)
:
ERROR Cannot invoke 'addToClients' with an argument list of type '(NSManagedObject)'
Definition of my two entities:
public class Post: NSManagedObject {
@nonobjc public class func postFetchRequest() -> NSFetchRequest<Post> {
return NSFetchRequest<Post>(entityName: "Post")
}
@NSManaged public var id: NSNumber
@NSManaged public var content: String
@NSManaged public var clients: NSSet?
func insertClientFor(url:String, completion: @escaping(Bool) -> ()) {
let managedContext = CoreDataManager.sharedManager.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Client",in: managedContext)!
let newClient = NSManagedObject(entity: entity, insertInto: managedContext)
newClient.setValue(url, forKey: "url")
self.addToClients(newClient) //ERROR Cannot invoke 'addToClients' with an argument list of type '(NSManagedObject)'
do {
try managedContext.save()
completion(true)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
completion(false)
}
}
}
// MARK: Generated accessors for clients
extension Post {
@objc(addClientsObject:)
@NSManaged public func addToClients(_ value: Client)
@objc(removeImagesStripObject:)
@NSManaged public func removeFromClients(_ value: Client)
@objc(addImagesStrip:)
@NSManaged public func addToClients(_ values: NSSet)
@objc(removeImagesStrip:)
@NSManaged public func removeFromClients(_ values: NSSet)
}
@objc(Client)
public class Client: NSManagedObject {
@NSManaged public var url: String
@NSManaged public var post: Post
@nonobjc public class func fetchRequest() -> NSFetchRequest<ImageStrip> {
return NSFetchRequest<ImageStrip>(entityName: "ImageStrip")
}
}
I don't understand what is wrong.
Have you tried casting newClient
to Client
?
let newClient = NSManagedObject(entity: entity, insertInto: managedContext) as? Client
or
let newClient = NSEntityDescription.insertNewObject(forEntityName: String(describing: Client.self), into: managedContext) as? Client