我有一个 UITableView
用UISearchbar
这让我可以根据 search text
过滤数据来自 UISearchbar
.
CoreData
表包含 3 attributes
name, notes, date
我想根据 User
在三列中搜索任何事件搜索文本。
所以我试了一下:
let searchText = searchText.lowercased()
let query = "name contains[cd] %@ OR notes contains[cd] %@ OR date contains[cd] %@"
let predicate = NSPredicate(format: query, searchText, searchText, searchText)
有什么方法可以一次性传递相同的参数( searchText
)?
类似于 Java
字符串格式化程序:
let query = "name contains[cd] %1$@ OR notes contains[cd] %1$@ OR date contains[cd] %1$@"
let predicate = NSPredicate(format: query, searchText)
最佳答案
您可以使用替换变量:
let searchText = searchText.lowercased()
let template = NSPredicate(format: "name contains[cd] $SRCH OR notes contains[cd] $SRCH OR date contains[cd] $SRCH")
let subVars = ["SRCH": searchText]
let predicate = template.withSubstitutionVariables(subVars)
参见 Apple Documentation 中的“使用谓词模板创建谓词” .
关于ios - 如何在 CoreData iOS 谓词生成器中多次传递相同的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170184/