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 回答