views:

30

answers:

1
+1  Q: 

Printing Core Data

I'm working on a program and I have created a fetch request to grab the data that I need to print. I'm able to log information like this:

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'>";
})

How do I seperate the properties like cueName, cueNo, cueNotes out to be printed?

Here is the fetch request:

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


}

Any help would be greatly appreciated.

Thank you,

Loren

A: 

You use basically the opposite of how you stored the values in your managedObject

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

if you've created a custom Class of your entity you could add this method:

- (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;
}

With this method you can just use NSLog(@"%@", object); to get a nice formatted output

fluchtpunkt
I actually used: NSString *name = [object valueForKey:@"cueName"]; but the idea was just the ticket! Thank you very much
loren zimmer