2009-08-03 18 views
2

以下のコードでは、フェッチ要求を作成しています。私が抱えている問題は、フェッチ要求データを取得し、それを変数に入れること(これは、最初のManagedObject変数を使って以下にしようとしているようなことです)を行うことです。つまり、私は "(エンティティ:質問; ID:0x10b6250 < - の形式ではない使用可能なデータを取得するために、私が作成した"結果 "NSArray(下のコードに示す) coredata:// 90FA9FD7-4CFC-4039-8A0C-40116055CADF/Question/p2; data:fault)」またはそれに類するもの。最後の行で作成したNSLogでは、executeFetchRequest(下の太字で示されている)から "結果" NSArrayをログに記録しますが、実際の "Question"テキストを取得するには次の手順を実行する方法がわかりません。どんな助けもありがとうございます。ありがとう。コアデータを取得する結果

のNSLog結果:

を要求getQuestionsByParentフェッチ:2見出さ(サブ変数:{ フォームID = "9822217D-6A55-4475-88EC-E2552B336E1B"; sectionNumber = 2;} 、結果:( (エンティティ:質問:id:0x10a8720 x-coredata:// 90FA9FD7-4CFC-4039-8A0C-40116055CADF/Question/p2; data:fault)、エンティティ:質問:ID:0x1092920 x-coredata:// 90FA9FD7- 4CFC-4039-8A0C-40116055CADF /質問/ p4;データ:障害))

コード:

NSManagedObjectContext *moc_ = [self managedObjectContext]; 

    NSMutableDictionary *dictionary=[[NSMutableDictionary alloc]init]; 
    [dictionary setObject:section.ordinal forKey:@"sectionNumber"]; 
    [dictionary setObject:section.parent.id forKey:@"formId"]; 

    NSError *error = nil;  
    NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel]; 
    NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"getQuestionsByParent" substitutionVariables:dictionary]; 
    NSAssert(fetchRequest, @"Can't find question fetch request"); 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ordinal" ascending:YES]; 
    NSArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    NSArray *result = [[NSArray alloc] init]; 
    result = [moc_ executeFetchRequest:fetchRequest error:&error]; 
    //return result; 

    NSLog(@"fetch request getQuestionsByParent: %u found (sub variables:%@, results:%@)", [result count], dictionary, result); 

    NSManagedObject *firstManagedObject = [result objectAtIndex:0]; 

答えて

3

データがあります。オブジェクトのカスタムサブクラスを実装している場合は、プロパティを照会するだけで済みます。そうでない場合は、KVCを使用してオブジェクトにアクセスできます。

NSManagedObject *firstManagedObject = [result objectAtIndex:0]; 
NSLog(@"firstObject.myAttribute: %@", [firstObject valueForKey:@"myAttribute"]); 

そして、あなたは、動的なプロパティとしての属性を宣言したオブジェクト(問)のカスタムサブクラスを持っている場合、それは次のようになります:

だから、例えば、あなたがして、「myAttribute」という属性を持っていると仮定
Question *firstQuestion = [result objectAtIndex:0]; 
NSLog(@"firstQuestion.myAttribute: %@", firstQuestion.myAttribute);