1

私はプログラムに取り組んでおり、印刷する必要があるデータを取得するためのフェッチ要求を作成しました。次のような情報をログに記録できます。

2010-10-03 16:57:10.362 lzshow7.2[2537:10b] <NSManagedObject: 0x2ca120> (entity: Song; id: 0x2afcb0 <x-coredata://CF5A85CE-BE0F-4ADC-979A-7F4214A8FB19/Song/p9> ; data: {
    cueName = Freedom;
    cueNo = 014;
    cueNotes = nil;
    songToInstrument = "<relationship fault: 0x2b1800 'songToInstrument'>";
})

cueName、cueNo、cueNotes などのプロパティを印刷するにはどうすればよいですか?

取得リクエストは次のとおりです。

 //Managed object context???
 NSLog(@"setting Managed object stuff");
 NSManagedObjectContext *context=[[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
 NSLog(@"Second line of Managed object stuff");






 //fetch request:  
 NSLog(@"Starting to fetch:");

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Song" inManagedObjectContext:context];
 [request setEntity:entity];
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cueNo" ascending:YES];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 [request setSortDescriptors:sortDescriptors];
 [sortDescriptors release];
 [sortDescriptor release];
 NSError *error;
 NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];



 for (id obj in mutableFetchResults)
  NSLog(@"%@", obj);

 NSLog(@"finished looping");
 //Error handling

 if (mutableFetchResults == nil) {

  // Handle the error.

 }

 //[self setEventsArray:mutableFetchResults];
 [mutableFetchResults release];
 [request release];


}

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

ありがとうございました、

ローレン

4

1 に答える 1

0

基本的に、managedObjectに値を格納した方法とは逆の方法を使用します

NSString *name = [song valueForKey:@"cueName"];
NSNumber *number = [song valueForKey:@"cueNo"];
NSString *notes = [song valueForKey:@"cueNotes"];
...
NSLog(@"%@ %@ %@", name, number, notes);

エンティティのカスタムクラスを作成した場合は、次のメソッドを追加できます。

- (NSString *)description {
    NSString *name = [song valueForKey:@"cueName"];
    NSNumber *number = [song valueForKey:@"cueNo"];
    NSString *notes = [song valueForKey:@"cueNotes"];
    ...
    NSString *returnString = [NSString stringWithFormat:@"%@ %@ %@", name, number, notes];
    return returnString;
}

このメソッドを使用NSLog(@"%@", object);すると、適切なフォーマットの出力を取得するために使用できます

于 2010-10-04T04:23:55.583 に答える