xcode 带有多个参数的 NSPredicate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6277921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSPredicate with Multiple parameters
提问by Rouslan Karimov
Hello Need help with predicate. The problem is that I want to have one function which will fetch from database depends on the multiple values it receives, but the values don't always exist, does it mean I have to create several different predicates?
您好 需要谓词方面的帮助。问题是我想要一个从数据库中获取的函数取决于它接收到的多个值,但这些值并不总是存在,这是否意味着我必须创建几个不同的谓词?
Code below
下面的代码
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND brand_id LIKE %@ AND item_category LIKE %@ AND item_make LIKE %@ AND item_gender LIKE %@ || item_price>%@ || item_price<%@",
brand_one, brand_two, brand_three, brand_four, brand_five, categoryString, makeString, genderString,[NSNumber numberWithInt:price_bottom],[NSNumber numberWithInt:price_top]];
brand_one, brand_two and etc sometimes exist and sometimes don't.
brand_one、brand_two 等有时存在,有时不存在。
And how should it for item_gender for example. Let's if there is no gender specified than to have both of them.
例如,对于 item_gender 应该如何处理。让我们如果没有指定性别而不是同时拥有它们。
Sorry if my description of the problem confusing.
抱歉,如果我对问题的描述令人困惑。
Base on Gobras comments the following code was produces
基于 Gobras 注释,生成了以下代码
NSArray *brands = [NSArray arrayWithObjects:brand_one, brand_two, brand_three, brand_four, brand_five, nil];
NSPredicate *predicate_brands = [NSPredicate predicateWithFormat:@"brand_id like %@" argumentArray:brands];
Logical explanation of what what search function should
什么搜索功能应该做的逻辑解释
- fetchrequest
- predicate on fetch request
- 获取请求
- 获取请求的谓词
predicate based on 5 brand ids, item id, item category, item make, item gender, if any of above is empty it should fetch all related entries for example if item category is empty it should fetch all "Jewellery, Watches and etc" but limit it with the rest of the predicate expressions.
基于 5 个品牌 ID、商品 ID、商品类别、商品品牌、商品性别的谓词,如果上述任何一项为空,则应获取所有相关条目,例如,如果商品类别为空,则应获取所有“珠宝、手表等”但用其余的谓词表达式来限制它。
Should I create for example compound predicates for brands and than create another one for item category with values "item_category like Jewellery" and "item_category like Watches" and after that how do I exactly bring them together?
我应该为品牌创建例如复合谓词,而不是为具有值“item_category like Jewellery”和“item_category like Watches”的项目类别创建另一个谓词,然后我如何将它们准确地组合在一起?
回答by Warren Burton
Assemble an appropriate collection of individual predicates into NSCompoundPredicate
将适当的单个谓词集合组装成 NSCompoundPredicate
as in
如
NSMutableArray *parr = [NSMutableArray array];
if([brand_one length]) {
[parr addObject:[NSPredicate predicateWithFormat:@"brand_id LIKE %@",myBrandId]];
}
if([brand_two length]) {
// etc
}
NSPredicate *compoundpred = [NSCompoundPredicate andPredicateWithSubpredicates:parr];
You can stack up your predicates with orPredicateWithSubpredicates:
and andPredicateWithSubpredicates:
and NSCompoundPredicate being a NSPredicate itself can be compounded.
你可以用orPredicateWithSubpredicates:
andandPredicateWithSubpredicates:
和 NSCompoundPredicate叠加你的谓词,作为一个 NSPredicate 本身可以复合。
See https://developer.apple.com/documentation/foundation/nscompoundpredicate
请参阅 https://developer.apple.com/documentation/foundation/nscompoundpredicate
回答by Rein rPavi
For those who are interested in Swift!
对于那些对 Swift 感兴趣的人!
let arrPred = [NSPredicate(format: "yourAttribute LIKE %@", toCompareID), NSPredicate(format: "yourAttribute2 LIKE %@", toCompareID2)] //Add as many predicates you want in short make your query
let compoundpred:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: arrPred)
var arrFiltered = yourArray.filteredArrayUsingPredicate(compoundpred)
回答by Gobra
It's pretty simple to build the predicate dynamically: combine your predicate format string with desired number of conditions and build corresponding NSMutableArraywith the arguments. [NSPredicate predicateWithFormat:argumentArray:]
will do the rest.
动态构建谓词非常简单:将谓词格式字符串与所需数量的条件相结合,并使用参数构建相应的NSMutableArray。[NSPredicate predicateWithFormat:argumentArray:]
会做剩下的。