需要
我必须找到rec_name为“Apple 256GB Macbook”的记录&isActive==true。
获取请求

fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] *%@* AND rec_name LIKE[cd] *%@* AND isActive = %@","apple","macbook",true)

错误
Unable to parse the format string "rec_name LIKE[cd] *%@* AND rec_name LIKE[cd] *%@* AND isActive = %@"

谢谢您,

最佳答案

问题是因为*%@*这里%@之间的*没有被一个字符串参数替换,所以NSPredicate无法进行查询LIKE *apple*LIKE *macbook*您需要做的是用通配符和Like创建一个搜索字符串,只需使用%@,同时@Vadian建议使用number而不是Bool

let searchkeyword1 = "apple"
let searchkeyword2 = "macbook"

//Make search string wildcards charaters
let searchString1 = "*\(searchkeyword1)*"
let searchString2 = "*\(searchkeyword2)*"

fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@", searchString1,searchString2, true as NSNumber)

另外,这里的batter选项是您可以使用CONTAINS而不需要使用通配符。
fetchRequest.predicate = NSPredicate(format: "rec_name CONTAINS[cd] %@ AND rec_name CONTAINS[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)

如果你想找到所有从苹果开始到macbook结束的记录,你还可以做一件事,然后你可以简单地使用BEGINSWITHENDSWITH
fetchRequest.predicate = NSPredicate(format: "rec_name BEGINSWITH[cd] %@ AND rec_name ENDSWITH[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)

关于ios - 谓词的核心数据获取请求错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43972780/

10-10 17:25