2012-11-24 61 views
0

我試圖編寫一個方法,該方法根據傳入的ID(NSInteger值)執行讀取操作。我可以形成一切的提取,但是當我添加謂詞時,程序崩潰。我做錯了謂詞嗎?在IOS中執行基於ID獲取

-(BOOL)finding:(NSInteger)theID 
{ 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self == %i",theID]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Course" inManagedObjectContext:_cdStack.managedObjectContext]; 

    [fetchRequest setEntity:entity]; 
    [fetchRequest setHavingPredicate:predicate]; 
    NSError *error = nil; 
    NSArray *fetchedObjects = [_cdStack.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
} 
+0

用實體的屬性名替換'self' – samfisher

+0

你看到了什麼崩潰?儘管@samfisher是正確的,謂詞中的「self」應該是屬性名稱。這可能是導致事故的原因。 – mattjgalloway

+0

我將自己更改爲ID,這是課程實體中的屬性。現在我得到一個錯誤,說明由於未捕獲的異常'NSInvalidArgumentException'導致終止應用程序,原因:'無效的獲取請求:HAVING沒有GROUP BY'。任何想法如何解決這個問題? – Joe24

回答

1

這個問題很舊,但我碰到了後續討論中提到的同樣的錯誤。因此,如果其他人最終遇到同樣的問題,則問題是使用[fetchRequest setHavingPredicate: predicate];而不是[fetchRequest setPredicate: predicate];