我在 coredata 中有名字、中间名和姓氏的“人”实体。现在,我想对全名(fn、mn、ln 的组合)执行谓词搜索。全名不是实体的属性。我正在尝试使用 nsfetchresultcontroller 获取。例如,如果我搜索“Jone Smith”,它会显示为空白,但如果我搜索“John”,它会显示记录。做回复。
问问题
184 次
2 回答
0
可能有不同数量的解决方案。其中之一是当我们试图找到一个全名的人时,我们不确定用户输入姓名的顺序是什么Name and Lastname
顺序Lastname and Name
。
斯威夫特 5.0
// First we make components from incoming search text `searchText`
let text = searchText.trimmingCharacters(in: .whitespaces)
let nameComponents = searchText.components(separatedBy: .whitespacesAndNewlines).compactMap { $0.isEmpty ? nil : $0 }
// Next we create predicate trying to find each component in name or lastname
let predicates = nameComponents.map { NSPredicate(format: "name contains[c] %@ OR lastname contains[c] %@", $0,$0) }
// Create `And` predicate
let andPredicate = NSCompoundPredicate.init(andPredicateWithSubpredicates: predicates)
// Use predicates in created `request`
request.predicate = andPredicate
同样,可以使用中间名。
于 2022-02-21T15:12:53.123 回答
-3
让我们假设字段名称是:fName、lName。那么谓词将是:
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"fName = %@ and lName = %@", inputFName, inputLName)]
于 2015-04-03T08:40:19.490 回答