2012-03-21 26 views
-3

我試圖獲取所有從一個特定的核心數據實體的數據,每個記錄放入一個字符串。但是,以下代碼僅打印它訪問的最後一條記錄。我究竟做錯了什麼?擷取來自實體的所有數據,並僅打印顯示的最後一條記錄

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext]; 

[fetchRequest setEntity:entity]; 
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 


NSLog(@"============================================="); 
NSLog(@"Generating Data To Be Sent"); 
for (NSManagedObjectContext * info in fetchedObjects) 

{ 

    NSLog(@"Client Name: %@", [info valueForKey:@"clientName"]); 
    NSLog(@"Client Account Number: %@", [info valueForKey:@"clientAccountNumber"]); 
    NSLog(@"Product Code: %@", [info valueForKey:@"productCode"]); 
    NSLog(@"Product Price: %@", [info valueForKey:@"productPrice"]); 
    NSLog(@"Product Quantity: %@", [info valueForKey:@"productQuantity"]); 
    NSLog(@"Order Total: %@", [info valueForKey:@"orderTotal"]); 
    NSLog(@"Order ID : %@", [info valueForKey:@"orderID"]); 
    NSLog(@"Transaction ID: %@", [info valueForKey:@"transactionID"]); 
    NSLog(@"Sales Rep ID : %@", [info valueForKey:@"salesRepresentativeID"]); 

} 

NSString * printedData; 
NSString * formattedData; 

NSString * clientName; 
NSString * clientAccount; 
NSString * productCode; 
NSString * productQuantity; 
NSString * productPrice; 
NSString * orderTotal; 
NSString * orderID; 
NSString * transactionID; 
NSString * salesRepID; 



for(NSManagedObject * info in fetchedObjects) 

{ 

    clientName = [info valueForKey:@"clientName"]; 
    clientAccount = [info valueForKey:@"clientAccountNumber"]; 
    productCode = [info valueForKey:@"productCode"]; 
    productPrice = [info valueForKey:@"productPrice"]; 
    productQuantity = [info valueForKey:@"productQuantity"]; 
    orderTotal = [info valueForKey:@"orderTotal"]; 
    orderID = [info valueForKey:@"orderID"]; 
    transactionID = [info valueForKey:@"transactionID"]; 
    salesRepID = [info valueForKey:@"salesRepresentativeID"]; 


    formattedData = [NSString stringWithFormat:@"|%@|%@|%@|%@|%@|%@|%@|%@|%@|\n",clientName,clientAccount,productCode,productQuantity,productPrice,orderID,transactionID,orderTotal,salesRepID]; 

    printedData = formattedData; 

} 

NSLog(@"============================================="); 
NSLog(@"Data Generated"); 
NSLog(@"%@",printedData); 
[fetchRequest release]; 

NSMutableArray *recipients = [[NSMutableArray alloc] initWithCapacity:1]; 
[recipients addObject:toEmail.text]; 

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
controller.mailComposeDelegate = self; 
[controller setSubject:toSubject.text]; 
[controller setMessageBody:printedData isHTML:NO]; 
[controller setToRecipients:recipients]; 

[self presentModalViewController:controller animated:YES]; 
[controller release]; 
+0

行'的NSLog(@ 「%@」,printedData);'應該是for循環 – Felix 2012-03-21 13:51:42

+0

確定,但那只是一個NSLog的,如果你能看到printedData變量在[控制器setMessageBody內:printedData isHTML:NO]; ,那什麼給我的問題,只有1個記錄被打印 – 2012-03-21 14:08:25

+0

字對字的重複[這個問題](http://stackoverflow.com/questions/9806668/printing-the-contents-of-a-fetchrequest)由同樣的問題。 – FluffulousChimp 2012-03-21 15:02:45

回答

1

要解決您的初始問題,您需要使用NSMutableString。不需要formattedData變量。

... 
NSMutableString * printedData = [NSMutableString string]; 
... 

for(NSManagedObject * info in fetchedObjects) 
{ 
    ... 
    [printedData appendFormat:@"|%@|%@|%@|%@|%@|%@|%@|%@|%@|\n",clientName,clientAccount,productCode,productQuantity,productPrice,orderID,transactionID,orderTotal,salesRepID]; 
} 
... 

你的下一個問題是這個代碼是多麼低效。您正在循環兩次相同的集合,一次記錄,一次創建格式化的字符串。你可以將它組合成一個循環。而且所有的變量都是在for循環的範圍之外定義的,你可以簡單的聲明每一行就像這樣。

for(NSManagedObject * info in fetchedObjects) 
{ 
    NSString *clientName = [info valueForKey:@"clientName"]; 
    NSString *clientAccount = [info valueForKey:@"clientAccountNumber"]; 
    ... 
    //Log them all 
    NSLog(@"%@",clientName); 
    NSLog(@"%@",clientAccount); 
    ... 
} 
+0

好不容易纔真正解決這個問題,我創建了一個陣列和存儲所有的值,它的工作原理,但我有一些不需要的文本,準確的說,它是: 階段:退出絲錐數:1窗口:> view:>位置在窗口:{428,471}在窗口之前的位置:{425,469}在視圖位置:{149,165}先前位置鑑於:{146,163}, – 2012-03-21 14:38:46

+0

時間開始調試,這意味着您在某處打印錯誤的對象。 – Joe 2012-03-21 14:43:12

+0

歡呼隊友欣賞指針:) – 2012-03-21 14:58:38

0

如果你想所有的formattedData字符串連接成一個單一的NSStringprintedData,這不是你的代碼是幹什麼的,對不對?在您的託管對象的每次迭代中,您將重新分配printedData。這就是爲什麼你只能得到最後的紀錄。

我推斷你想要打印的數據來累積從你的NSManagedObject實例派生的所有formattedData字符串。如果是這樣的話,那麼你需要在你的管理對象,如申報printedDataNSMutableString和追加formattedData字符串是可變的字符串在每次迭代

for(NSManagedObject *info in fetchedObjects) 
{ 
    formattedData = [NSString stringWithFormat://blah blah blah 
    [printedData appendString:formattedData]; 
} 
+0

這是工作,直到它擊中[排列ADDOBJECT:printedData] 錯誤EXC_BAD_ACCESS ...任何想法?我把這個代碼放在print data =格式化的數據後面; – 2012-03-21 15:28:30

+0

如何聲明'array'? – FluffulousChimp 2012-03-21 17:19:46

相關問題