Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here") Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here") ios ios

Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here")


You're trying to compare a collection (categories.name) to a scalar value (category.name). You need to either use a collection comparator (CONTAINS), or use a predicate modifier (ANY/ALL/SOME, etc).

Try using:

[NSPredicate predicateWithFormat:@"ANY categories.name =[cd] %@", category.name];

Or:

[NSPredicate predicateWithFormat:@"categories.name CONTAINS[cd] %@", category.name];


SWIFT SYNTAX

In case anyone happens upon this writing in swift as I did...

let predicate = NSPredicate(format: "ANY categories.name = %@", category.name!)fetchRequest.predicate = predicate

worked for me.


For those who faced the same problem. You can use IN operator instead of = / == to look for specific objects in any Collection.Look here for reference. No difference Swift or Objective-C