Although I know that this question is frequently asked, I haven't found a prober solution for my case. I am working on app that uses core data. In core data , I have an entity named User Ent that has a one to many relationship with an entity named Hefth Ent. Hefth Ent has the reverse relationship that is to one user ent. I am trying to modify the set of hefths objects in the user but I always get this error : setOwnerUser:]: unrecognized selector sent to instance 0x7fd2d25dae90 Although I have tried the suggested solutions in the other questions like converting the set into mutable set and then mutating it but that didn't work with me. here is the User Ent class :
import Foundation
import CoreData
extension UserEnt {
@NSManaged var name: String?
@NSManaged var numOfKhetma: NSNumber?
@NSManaged var profilePic: NSData?
@NSManaged var remPagesForCurrentKhetma: NSNumber?
@NSManaged var currentHefth: HefthEnt?
@NSManaged var currentMoraj3a: Moraj3aEnt?
@NSManaged var hefthSejil: NSSet?
}
and here is the hefth ent class...`import Foundation import CoreData
extension HefthEnt {
@NSManaged var achivingPercentage: NSNumber?
@NSManaged var endDate: NSDate?
@NSManaged var numOfPages: NSNumber?
@NSManaged var remPages: NSNumber?
@NSManaged var title: String?
@NSManaged var ownerUser: UserEnt?
}
where ownerUser is the user that has many of this hefth object and here is the last code I used trying to add hefth to user and I still get the same error....
func addHefthToSejil(hefth : HefthObj, forUserOfIndex userIndex:Int)
{
let request = NSFetchRequest(entityName: "UserEnt")
let users = (try! context.executeFetchRequest(request)) as! [UserEnt]
let hefthEnt = NSEntityDescription.insertNewObjectForEntityForName("HefthEnt", inManagedObjectContext: context) as! HefthEnt
hefthEnt.numOfPages = hefth.numOfPages
hefthEnt.endDate = hefth.endDate
hefthEnt.remPages = hefth.numOfPages
hefthEnt.achivingPercentage = hefth.achievingPercentage
hefthEnt.ownerUser = users[userIndex]
users[userIndex].mutableSetValueForKey("hefthSejil").addObject(hefthEnt)
// the problem happens here
do
{
try context.save()
}catch
{
print("error")
}
updateAllUsers()
}
I have tried many things and nothing worked with me, hope I find a solution to my issue here.
Try this:
let user = users[userIndex] as? UserEnt
let relationship = user.valueForKeyPath("hefthSejil") as? NSMutableSet
relationship.addObject(hefthEnt)