Вы можете найти команду базы данных, чтобы получить этот вид поиска.В swift легко создать такой предикат, как показано ниже, если я правильно понимаю ваше требование.
let multipleSearchString = "my stg l la ma"
let texts = ["mystringlladm1a", "mystr2ingllama", "mystri2ngllama", "mys3ringllama"]
let key = multipleSearchString.compactMap{ $0 == " " ? nil : String($0) + "*"}.joined()
let predicate = NSPredicate(format: "SELF like[c] %@", argumentArray: [key])
print (texts.filter{predicate.evaluate(with: $0)})
//["mystringlladm1a", "mystr2ingllama", "mystri2ngllama"]
В вашем добавленном случае для достижения вашей цели можно добавить вычисляемое свойство:
// extension Price{
// var lowerCasedMultipleSearchString: String{
// if let string = self.MultipleSearchString?.lowercased(){
// return string
// }
// return ""
// }
// }
// func searchTextInPrices(prices: Results<Price>, text: String) -> //Results<Price> {
// let key = text.compactMap{ $0 == " " ? nil : String($0) + "*"}.joined()
// let predicate = NSPredicate(format: "SELF like[c] %@", argumentArray: [key])
// return prices.filter{predicate.evaluate(with: $0.lowerCasedMultipleSearchString)}
// }
func searchFor(text: String) {
if text == "" || text.count == 0 {
loadPricesFromDb()
}
else {
let realm = try! Realm()
self.items = []
let prices = realm.objects(Price.self)
//The changed codes here.
let key = text.compactMap{ $0 == " " ? nil : String($0) + "*"}.joined()
let predicate = NSPredicate(format: "SELF like[c] %@", argumentArray: [key])
let results = prices.filter{predicate.evaluate(with: ($0.MultipleSearchString?.lowercased())!)}
//second edition:
var result = prices
var length : Int = 0
repeat{
let key = text[...(text.index(text.startIndex, offsetBy: length))].compactMap{ $0 == " " ? nil : String($0) + "*"}.joined()
let predicate = NSPredicate(format: "SELF like[c] %@", argumentArray: [key])
results = prices.filter{predicate.evaluate(with: ($0.MultipleSearchString?.lowercased())!)}
length += 1} while(length < text.count)
//Third edition:
let results = prices.filter{ text.reduce((($0.MultipleSearchString?.lowercased())!, true)){
if !$0.1 {return ("",false)}
else if let index = $0.0.index(of: $1) {return (String($0.0[index...]),true)}
return ("",false)
}.1}
self.items.append(contentsOf: results)
self.tableView.reloadData()
}
}