我试图获取所有从一个特定的核心数据实体的数据,每个记录放入一个字符串。但是,以下代码仅打印它访问的最后一条记录。我究竟做错了什么?撷取来自实体的所有数据,并仅打印显示的最后一条记录
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];
行'的NSLog(@ “%@”,printedData);'应该是for循环 – Felix 2012-03-21 13:51:42
确定,但那只是一个NSLog的,如果你能看到printedData变量在[控制器setMessageBody内:printedData isHTML:NO]; ,那什么给我的问题,只有1个记录被打印 – 2012-03-21 14:08:25
字对字的重复[这个问题](http://stackoverflow.com/questions/9806668/printing-the-contents-of-a-fetchrequest)由同样的问题。 – FluffulousChimp 2012-03-21 15:02:45