0

既にカテゴリにある値を wod エンティティにリンクしようとしています。カテゴリの wod のレコードごとに新しいレコードを呼び出したいので。これを行う方法がわからない。述語を使用することを考えていましたが、フェッチ要求からリンクする方法が正確にはわかりません。

これは私のスキーマがどのように見えるかです:

ここに画像の説明を入力

それらをリンクしようとするコードは次のとおりです。

 NSManagedObjectContext *context = [self managedObjectContext];
    Wod *wodInfo = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Wods" 
                                       inManagedObjectContext:context];
    [wodInfo setValue:@"Frank" forKey:@"name"];


    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:[NSEntityDescription entityForName:@"Categories"
                                   inManagedObjectContext:context]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"(name == %@", @"Time"]];

    // This is the part where i am unsure, since i am not exactly sure how to link them up
      Category *category = reques
      wodInfo.category = 



    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

どんな助けでも大歓迎です。

4

1 に答える 1

0
NSError *error = nil;
NSArray *categories = [context executeFetchRequest:request error:&error];
Category *category = [categories lastObject];
wodInfo.category = category;

への呼び出しには注意してください[context save:&error]。コードで初期化されず、ランダムなアドレスを参照するエラー変数のアドレスを渡しているため、アプリケーションで奇妙なエラーが発生する可能性があります。save: メソッドに渡す前に nil に設定することをお勧めします。

于 2011-06-17T05:32:22.120 に答える