0
我有一個核心數據模型,有一對多的關係,有一個類別,它可以包含許多子類別。核心數據謂詞對許多
類別< ---- >>子目錄
我試圖執行一個取來檢查,如果一個特定的類別中包含有特定名稱的子目錄。假設我有以下兩個類別,我想要查看名爲「Fruits」的類別中是否有任何子類別名稱爲「Apple」。
Vetegables
- Carrot
- Lettuce
Fruits
- Apple
- Orange
- Pear
代碼:
- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
inCategory:(Category *)category
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SubCategory" inManagedObjectContext:self.beer.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", subCategory];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects != nil && fetchedObjects.count > 0) {
return [fetchedObjects objectAtIndex:0];
}
else {
return nil;
}
}
:)對我來說看起來不錯,那麼你的問題是什麼? – Tobi
NSPredicate * predicate = [NSPredicate predicateWithFormat:@「name == [c]%@ and category ==%@」,subCategory,category]; – NeverBe