2012-11-27 133 views
1

所以我想解決如何使用這些謂詞,我已經閱讀了Apple文檔並試圖使用它(https://developer.apple.com/library/ mac /#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html),並且我已經設置了謂詞,但它不斷得到線程1:EXC_BAD_ACCESS(code =)等等。核心數據謂詞與鍵路徑

NSError *error; 
    NSLog(@"1"); 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fruit" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 
    NSLog(@"2"); 
    NSPredicate *predicate = [NSPredicate predicateWithFormat: 
           @"Source.sourceName contains[cd] %@", "Apple Tree"]; 

    [fetchRequest setPredicate:predicate]; 
    NSLog(@"3"); 
    NSArray *fetchResult = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    NSLog(@"4"); 
    testLbl.text = [fetchResult objectAtIndex:0]; 

,這是我正在使用的代碼,作爲核心數據,我們有......

實體水果&來源

屬性fruitName & SOURCENAME

關係(一對一)fruitSource < ---------> sourceFruit

我想要什麼要做的是拔出來自蘋果樹的任何水果...>。 <

回答

1

有兩個不同的問題:

  1. Fruit得到你必須使用的關係相關Source@"fruitSource.sourceName contains ..."代替@"Source.sourceName contains ..."

  2. (這可能導致該異常。)的%@格式需要一個Objective-C的對象作爲參數,而不是一個C字符串:@"Apple Tree"代替"Apple Tree"

所以謂語應該是這樣的:

[NSPredicate predicateWithFormat:@"fruitSource.sourceName CONTAINS[cd] %@", @"Apple Tree"] 
+0

謝謝!血腥的@符號,它總是一些不是嗎?!感謝您的建議:D – Craig