[英]Core Data: Matching a combination of multiple items in a to-many relation
編輯:忘了提到我后來在 Swift 代碼中過濾掉了帶有不需要顏色的卡片。
因此,這是關於 Core Data 中的多對多關系以及如何為其編寫謂詞的另一個問題。 簡而言之,我想在一對多關系中匹配多個項目的組合。
搜索黑色和/或白色的卡片,意思是:
最佳結果是(簡化):
NSPredicate(format: "ANY color == Black") // Only black cards, good
NSPredicate(format: "ANY color == White") // Only white cards, good
NSPredicate(format: "ANY color == Black OR ANY color == White") // Only black AND white cards, bad
這里有一篇與 MySQL 相關的非常相似的帖子,以防它有助於進一步澄清問題:
根據@pbasdf的評論,我提出了以下解決方案(在實際代碼中使用更好的語法):
// Desired colors let includePredicate = NSPredicate(format: “SUBQUERY(color, $C, $C == 'Black' OR $C == 'White').@count > 0”)
// Undesired colors let excludePredicate = NSPredicate(format: “SUBQUERY(color, $C, $C == 'Green' OR $C == 'Red' OR $C == 'Blue').@count == 0”)
// Combined to one predicate let finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [includePredicate, excludePredicate]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.