ios NSArray 和 NSPredicate 使用 NOT IN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8580715/
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
NSArray with NSPredicate using NOT IN
提问by Slee
I have an NSArray that I want to filter out certain objects using an NSPredicate, I was hoping I could use NOT IN since I saw that I can easily do an IN.
我有一个 NSArray,我想使用 NSPredicate 过滤掉某些对象,我希望我可以使用 NOT IN,因为我看到我可以轻松地执行 IN。
So I have my array:
所以我有我的数组:
self.categoriesList
Then I get the values I want to remove:
然后我得到我想要删除的值:
NSArray *parentIDs = [self.cateoriesList valueForKeyPath:@"@distinctUnionOfObjects.ParentCategoryID"];
This gives me a list of ParentCategoryID's for categories I DO NOT want to display, so I figure I can use an NSPredicate to remove them:
这为我提供了我不想显示的类别的 ParentCategoryID 列表,所以我想我可以使用 NSPredicate 来删除它们:
self.cateoriesList = [self.cateoriesList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"CategoryID NOT IN %@",parentIDs]];
This fails:
这失败了:
reason: 'Unable to parse the format string "CategoryID NOT IN %@"'
If I wanted to use just IN that works perfectly of course.
如果我只想使用 IN,那当然可以完美运行。
回答by dreamlax
What about NOT (CategoryID IN %@)
?
怎么样NOT (CategoryID IN %@)
?
回答by Mark Adams
How about using NONE
?
怎么用NONE
?
[NSPredicate predicateWithFormat:@"NONE CategoryID IN %@", parentIDs];