从for语句创建txt文件时遇到问题。以下是CoreData上语句的代码。
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * allTickets = [[NSFetchRequest alloc] init];
[allTickets setEntity:[NSEntityDescription entityForName:@"Place" inManagedObjectContext:context]];
[allTickets setIncludesPropertyValues:NO];
NSError * error = nil;
NSArray * ticks = [context executeFetchRequest:allTickets error:&error];
for (NSManagedObject * tick in ticks) {
NSArray *keys = [[[tick entity] attributesByName] allKeys];
NSDictionary *myDict = [tick dictionaryWithValuesForKeys:keys];
NSString *theDate = [myDict objectForKey:@"date"];
NSString *theName = [myDict objectForKey:@"name"];
NSString *theNumber = [myDict objectForKey:@"number"];
NSString * finalExport = [NSString stringWithFormat:@"%@%@%@", theDate, theName, theNumber];
NSLog(@"%@",finalExport);
NSLog结果正确打印出CoreData中的所有条目,一个接一个地运行每个请求。
我的问题是,当我想用finalExport NSString创建一个txt时,我只在文件中有第一个请求。
例如,我得到了一个NSLog:
01.01.2015 MYNAME 34555445
02.01.2015 MYNAME 34523445
03.01.2015 MYNAME 34115445
04.01.2015 MYNAME 34552345
但是在文本文件中我只得到了 01.01.2015 MYNAME 34555445
那我怎么能有一个包含所有NSLog结果的文本文件
先谢谢你的帮助;)
EDIT ++++++++++++++++++++++
以下是代码的其余部分:
NSFetchRequest * allTickets = [[NSFetchRequest alloc] init];
[allTickets setEntity:[NSEntityDescription entityForName:@"Place" inManagedObjectContext:context]];
[allTickets setIncludesPropertyValues:NO];
NSError * error = nil;
NSArray * ticks = [context executeFetchRequest:allTickets error:&error];
for (NSManagedObject * tick in ticks) {
NSArray *keys = [[[tick entity] attributesByName] allKeys];
NSDictionary *myDict = [tick dictionaryWithValuesForKeys:keys];
NSString *theDate = [myDict objectForKey:@"date"];
NSString *theName = [myDict objectForKey:@"name"];
NSString *theNumber = [myDict objectForKey:@"number"];
NSString * finalExport = [NSString stringWithFormat:@"%@%@%@", theDate, theName, theNumber];
NSLog(@"%@",finalExport);
答案 0 :(得分:0)
您需要确保为文件写入一个条目数组,而不仅仅是一个条目。