I did not like the accepted answer because it actually did not answer the question ... but then it helped me more than I understood. Now I will use closure instead of NSPredicates. The actual answer to this question should be a slightly modified version of @NSGangster's answer:
let realm = try! Realm() //Array of publications let realmObjects = realm.objects(Publication) //any publication where .text property == special will be filtered. and filter out empty array let filterThis = realmObjects.filter({ $0.typez.filter({ $0.text == "special" } != [] ) }) print(filterThis)
.. or something close to that.
But what I was looking for was a little different. I need a way to filter on the exact words of a verbose string, and using NSPredicate with "CONTAINS" would match any contained substring, for example. a search for "red" will match "fred". The kingdom does not yet support LIKE or regex, so using closure was the only way I could work:
//I was going for a "related terms" result for a dictionary app let theResults = terms.filter( { //Looking for other terms in my collection that contained the //title of the current term in their definition or more_info strings $0.definition.components(separatedBy: " ").contains(term.title) || $0.more_info.components(separatedBy: " ").contains(term.title) } )
With so many days when I searched, we hope this helps someone else with a similar problem.
source share