Learn Courses My Dashboard

Coredata: NSPredicate to filter matching object in NSSet

In relation, to my question:

My problem here is, how to create a predicate when working with “many to many” relations.
For example, I would like to return all instances of words that matches 1 tag (or more tags)

I will give a try here

let request: NSFetchRequest<Words> = Words.fetchRequest()
let predicate = NSPredicate(format: "withTagsOf CONTAINS %@", tag). // where tag is NSObject instance of Tags 

I know that CONTAINS is for String Comparison, I just don’t know what to use if it is array

Then what more, if I need to get words that matches multiple tags?

Detail for reference:
I sumarized my 2 entity here

extension Words {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Words> {
        return NSFetchRequest<Words>(entityName: "Words")
    }

    @NSManaged public var character: String?
    @NSManaged public var id: UUID?
    //..... I removed other properties to simplify
    @NSManaged public var withTagsOf: NSSet?
    @NSManaged public var hasExamplesOf: NSSet?

}

extension Tags {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Tags> {
        return NSFetchRequest<Tags>(entityName: "Tags")
    }

    @NSManaged public var category: Int16
    @NSManaged public var id: UUID?
    @NSManaged public var name: String?
    @NSManaged public var withWordsOf: NSSet?

}

// 


Thanks in advance

I Still could not find solution for this:

I used something like this but I always end up with runtime error

dB.gPredicate = NSPredicate(format: "ANY withTagsOf.name == %@", dB.tags[memoTagsSelectIdx].name!)
// or
dB.gPredicateHidden = NSPredicate(format: "withTagsOf == %@ && isHidden == false", dB.tags[memoTagsSelectIdx])

withTagsOf is NSSet , basically an array of Class Tags
Then I want to get all words that has Tags of 1 or more tags

I realize I can just access each individual Tags (without using fetch), and just return the words inside it
but I wanted to unify my applications pattern of implementation

my case is similar to this:

but her solution doesn’t works for me

I would appreciate any help or clues.
Thanks in advance