我有一個模型與關係屬性文章的類別是一個NSOrderedSet。 現在我想要得到與在一定條件滿足的文章的所有類別,在SQL我會寫:一對多過濾器導航屬性
SELECT *
FROM Category AS cat
JOIN Article AS art ON art.categoryId = cat.categoryId AND art.gender='m';
我試着用:
NSPredicate(format: "articles.gender like %@ OR articles.gender = %@", gender.lowercased(), "n")
我收到以下錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
完整代碼:
let ctx = self.Context()
var gender = UserDefaults.standard.string(forKey: "gender_preference") ?? "*"
if gender.uppercased() == "N" { gender = "*" }
// Create Fetch Request
let fetchRequest: NSFetchRequest = ArticleCategory.fetchRequest()
let predicate = NSPredicate(format: "articles.gender like %@ or articles.gender = %@", gender, "n")
fetchRequest.predicate = predicate
// sort by name
let sort = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sort]
do {
let result = try ctx.fetch(fetchRequest)
return result
} catch {
print(error)
}
return []
親切的問候
編輯:
你試過用'ANY'? 'NSPredicate(format:「ANY articles.gender like%@ OR articles.gender =%@」,gender.lowercased(),「n」)' – mat
對不起,是的,我試過了,對於一些和所有的都一樣 – Mario