核心数据以关键路径作为谓词

时间:2012-11-27 13:57:57

标签: xcode cocoa-touch core-data relationships predicates

所以我正在尝试研究如何使用这些谓词,我已阅读Apple文档并尝试使用它(https://developer.apple.com/library/mac/#documentation/Cocoa/ Conceptual / Predicates / Articles / pUsing.html)并且我设置了谓词,但它继续获得线程1:EXC_BAD_ACCESS(code =)etc.etc。

    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

我想要做的是取出任何来自Apple Tree的水果......>。<

1 个答案:

答案 0 :(得分:1)

有两个不同的问题:

  1. 要从Fruit转到相关的Source,您必须使用关系:@"fruitSource.sourceName contains ..."而不是@"Source.sourceName contains ..."

  2. (这可能导致异常。)%@格式需要Objective-C对象作为参数,而不是C字符串:@"Apple Tree"而不是"Apple Tree"。< / p>

  3. 所以谓词应该是这样的:

    [NSPredicate predicateWithFormat:@"fruitSource.sourceName CONTAINS[cd] %@", @"Apple Tree"]
    
相关问题