打印核心数据

问题描述 投票:0回答: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];
 
}
core-data printing nsfetchrequest
1个回答
0
投票

您使用的方式基本上与在托管对象中存储值的方式相反

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);
即可获得漂亮的格式化输出

© www.soinside.com 2019 - 2024. All rights reserved.