let selectedConsoles = ["Xbox", "Playstation 4"]
let players = realm.objects(Person).filter("consoles IN %@", selectedConsoles)假设播放器的属性控制台是一个List<console>()
所以我想过滤所有同时拥有xbox和PlayStation4的玩家。目前我可以通过OR比较来过滤它们,我的目标是实现and比较,例如目前它只检查"xbox“或”PlayStation4“是否存在于玩家的控制台中。我想退还有两个游戏机的播放器。如有任何帮助或提示,我们将不胜感激
发布于 2017-07-27 06:23:02
要获得拥有这两种类型的控制台的用户,您需要对其进行"and“操作。如果想要选择那些具有其中之一的对象,则需要将它们“或”在一起。如果你“和”它们,你必须同时拥有它们,否则它将不会被包括在内;如果你“或”它们,它将被包括在其中存在的情况下。
所以你需要创建一个复合的"or“谓词。您可以为要包含的每个case创建一个谓词,然后使用复合谓词将所有谓词" or“或" and”放在一起。
我根据您上面提供的内容进行了一些猜测,但这应该非常接近。如果它需要澄清或清理,请告诉我。它可以编译,但我没有为它创建测试数据集。用您的变量名(表示您的领域对象)和类名替换。
let selectedConsoles = ["Xbox", "Playstation 4"]
let predicateOne = NSPredicate(format:"consoles IN %@", [selectedConsoles[0]])
let predicateTwo = NSPredicate(format:"consoles IN %@", [selectedConsoles[1]])
let compoundPredicateEitherConsole = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateOne, predicateTwo])
let compoundPredicateBothConsoles = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateOne, predicateTwo])
let results = <realm>.objects(<YourClassName>.self).filter(compoundPredicateEitherConsole)https://stackoverflow.com/questions/45338083
复制相似问题