打印核心数据

时间:2010-10-03 21:25:33

标签: core-data printing core nsfetchrequest

我正在开发一个程序,我已经创建了一个获取请求来获取我需要打印的数据。我能够记录这样的信息:

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];


}

非常感谢任何帮助。

谢谢,

洛伦

1 个答案:

答案 0 :(得分: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);即可获得精美的格式化输出