CoreData Predicate get every sentence that contains any word in array

此生再无相见时 提交于 2019-12-10 15:53:41

问题


In Swift 4 I have a CoreData "Sentence" model that has a String attribute "englishsentence". I also have an array of "words" and would like to fetch all sentences for which the "englishsentence" attribute contains one or more of the words in the array.

var words = ["today", "yesterday", "tomorrow"]

This array is just an example. It is supposed to change at runtime and can have any length.

and in the fetch request I am trying to do something like this:

let fetchRequest =
    NSFetchRequest<NSManagedObject>(entityName: "Sentence")
let predicate = NSPredicate(format: "ANY englishsentence CONTAINS ANY word IN %@", words)
    fetchRequest.predicate = predicate

I am able to create a predicate for all sentences that contain one particular word. However, I cannot get it to work with an array of words unless of course I iterate through the words-array and make a new fetch request for every single word. But this seems awfully inefficient.


回答1:


One option would be to create a “compound predicate:”

let words = ["today", "yesterday", "tomorrow"]

let predicates = words.map {
    NSPredicate(format: "englishsentence CONTAINS %@", $0)
}
let predicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)

Another option is to match against a regular expression:

let regex = ".*(" + words.map {
    NSRegularExpression.escapedPattern(for: $0)
}.joined(separator: "|") + ").*"

let predicate = NSPredicate(format: "englishsentence MATCHES %@", regex)

To match only “whole words” you can add the word boundary pattern \b:

let regex = ".*\\b(" + words.map {
    NSRegularExpression.escapedPattern(for: $0)
}.joined(separator: "|") + ")\\b.*"


来源:https://stackoverflow.com/questions/50085345/coredata-predicate-get-every-sentence-that-contains-any-word-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!