2010-10-03 70 views
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]; 


} 

任何幫助將不勝感激。

謝謝

羅蘭

回答

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);來得到一個很好的格式化輸出

+0

我實際使用:NSString * name = [object valueForKey:@「cueName」];但這個想法只是票!非常感謝你 – 2010-10-05 01:15:42

+0

但是,蘋果不鼓勵重寫'-description'方法嗎? http://stackoverflow.com/a/2170679/294661 – 2012-11-12 16:26:57