需要
我必须找到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 = %@"
谢谢,
答案 0 :(得分:1)
问题是由于*%@*
%@
之间的*
未被字符串参数替换,因此NSPredicate
无法进行查询LIKE *apple*
}和LIKE *macbook*
您需要做的是使用通配符创建搜索字符串,而使用Like只需使用%@
,同时@Vadian建议使用数字而不是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)
此处的击球选项是你可以使用CONTAINS
而不需要使用通配符。
fetchRequest.predicate = NSPredicate(format: "rec_name CONTAINS[cd] %@ AND rec_name CONTAINS[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)
如果你想找到所有以apple开头的记录并以macbook结尾,你可以再做一件事,那么你可以简单地使用BEGINSWITH
和ENDSWITH
fetchRequest.predicate = NSPredicate(format: "rec_name BEGINSWITH[cd] %@ AND rec_name ENDSWITH[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)
答案 1 :(得分:0)
试试这个
fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true))
答案 2 :(得分:0)
您需要设置如下的谓词查询:
fetchRequest.predicate = NSPredicate(format: "rec_name == %@ AND isActive == %@" , "Apple 256GB Macbook", NSNumber(booleanLiteral: true))
如果您正在寻找完美匹配,我认为我们不需要使用Like。
答案 3 :(得分:0)
试试这个
NSPredicate(format: "rec_name == %@ AND rec_name == %@ AND isActive = %d","apple","macbook",true)