1

だから私はこれらの述語を使用する方法を考え出そうとしています. /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

関係 (1 対 1) fruitSource<--------->sourceFruit

私がやりたいのは、りんごの木から実がなれば、それをすべて抜き取ることです... >.<

4

1 に答える 1

1

2 つの異なる問題があります。

  1. Fruit関連するから取得するには、 の代わりにSource関係: を使用する必要があります。@"fruitSource.sourceName contains ..."@"Source.sourceName contains ..."

  2. (これがおそらく例外の原因です。) この%@形式では、C 文字列ではなく、Objective-C オブジェクトを引数として必要とします:@"Apple Tree"の代わりに"Apple Tree".

したがって、述語は次のようになります。

[NSPredicate predicateWithFormat:@"fruitSource.sourceName CONTAINS[cd] %@", @"Apple Tree"]
于 2012-11-27T14:46:04.843 に答える