从实体中提取所有数据并仅打印显示最后一条logging

我试图从一个特定的核心数据实体获取所有的数据,并将每个logging放入一个string。 但是下面的代码只打印它访问的最后一个logging。 我究竟做错了什么?

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

要解决你最初的问题,你需要使用一个NSMutableString 。 不需要formattedDatavariables。

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

你的下一个问题是这个代码是多么的低效。 您正在循环两次相同的集合,一次logging,一次创build格式化的string。 你可以把它组合成一个循环。 而且所有的variables都是在for循环的范围之外定义的,你可以直接声明每一行就像这样。

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

如果您试图将所有formattedDatastring连接成一个NSString printedData ,那不是你的代码在做什么,对吧? 在您的托pipe对象的每次迭代中,您将重新分配printedData 。 这就是为什么你只能得到最后的logging。

我推断你想要打印的数据来积累从NSManagedObject实例派生的所有formattedDatastring。 如果是这样的话,那么你需要将printedData声明为一个NSMutableString并在每次迭代中将formattedDatastring附加到该可变string上,例如:

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