Pre*_*van 14 csv cocoa-touch objective-c ipad ios
我想将数据从sql文件写入csv文件.我已从数组中的sql文件中收集了所有数据并使用for循环我将数据附加到.csv文件中.但它似乎只在一行中显示数据,但它没有转到新行来创建新行.我用这个作为参考.这是我的代码:
-(NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"myfile.csv"];
}
- (IBAction)saveAsFileAction:(id)sender {
if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
[[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil];
NSLog(@"Route creato");
}
NSString *writeString;
for (int i=0; i<[dataArray count]; i++) {
writeString = [NSString stringWithFormat:@"%@, %@, %@, %@, %0.2f,",[[dataArray objectAtIndex:i]dates],[[dataArray objectAtIndex:i] time],[[dataArray objectAtIndex:i] category],[[dataArray objectAtIndex:i]place],[[dataArray objectAtIndex:i] amount]];
NSLog(@"writeString :%@",writeString);
NSFileHandle *handle;
handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];
//say to handle where's the file fo write
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
//position handle cursor to the end of file
[handle writeData:[writeString dataUsingEncoding:NSUTF8StringEncoding]];
}
}
Run Code Online (Sandbox Code Playgroud)
sos*_*orn 36
这只会写一行,因为每次循环都会重写文件.writeData
在循环完成之前,最好不要在文件上.我也会像这样使用NSMutableString:
- (IBAction)saveAsFileAction:(id)sender {
if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
[[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil];
NSLog(@"Route creato");
}
NSMutableString *writeString = [NSMutableString stringWithCapacity:0]; //don't worry about the capacity, it will expand as necessary
for (int i=0; i<[dataArray count]; i++) {
writeString = [writeString appendString:[NSString stringWithFormat:@"%@, %@, %@, %@, %0.2f, \n",[[dataArray objectAtIndex:i]dates],[[dataArray objectAtIndex:i] time],[[dataArray objectAtIndex:i] category],[[dataArray objectAtIndex:i]place],[[dataArray objectAtIndex:i] amount]]]; //the \n will put a newline in
}
}
//Moved this stuff out of the loop so that you write the complete string once and only once.
NSLog(@"writeString :%@",writeString);
NSFileHandle *handle;
handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];
//say to handle where's the file fo write
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
//position handle cursor to the end of file
[handle writeData:[writeString dataUsingEncoding:NSUTF8StringEncoding]];
}
Run Code Online (Sandbox Code Playgroud)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *root = [documentsDir stringByAppendingPathComponent:@"customers.csv"];
NSString *temp;
temp = [NSString stringWithFormat:@"%@", [arrCustomersName objectAtIndex:0]];
for (int i = 1; i < [arrCustomersName count]; i++) {
temp = [temp stringByAppendingFormat:@", %@", [arrCustomersName objectAtIndex:i]];
}
[temp writeToFile:root atomically:YES encoding:NSUTF8StringEncoding error:NULL];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25365 次 |
最近记录: |