I want to search keywords in Hotels Entity in two attributes (hotelName & cityName)
i'm looking for hotel
"The Kensington Studios" is in hotelName Attribute
"London" is cityName Attribute
in search bar, if i type middle of the hotelName and cityName(just like below) it should identify and display the results.
to achieve this, i'm doing like
let charSet = CharacterSet(charactersIn: " ")
let words = searchText.components(separatedBy: charSet)
let predicate = NSPredicate(format: "(hotelName IN %@) OR (cityName IN %@)", words, words)
But its giving empty results.
Please guide me..
words object returns [String] array. You have to use each object of the array. So the code should be like
let words = searchText.components(separatedBy: " ")
var predicateArr = [NSPredicate]()
for word in words! {
let predicate = NSPredicate(format: "(hotelName contains [c]) OR (cityName contains [c])", word, word)
predicateArr.append(predicate)
}
let compound = NSCompoundPredicate(orPredicateWithSubpredicates: predicateArr)
let output = array.filtered(using: compound)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With