我創建了一個應用程序以保存爲csv文件,應用程序在iOS模擬器中同時可以讀寫。當我將應用程序加載到iOS設備時,它不包含數據。在iOS設備上讀取csv文件時出錯
- (IBAction)saveInfo:(id)sender {
NSString *resultLine = [NSString stringWithFormat:@"%@,%@,%@\n",
self.food.text,
self.movies.text,
self.channel.text];
NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
//resultView.text = docPath;
NSString *filename = [docPath stringByAppendingString:@"result.csv"];
resultView.text = filename;
if (![[NSFileManager defaultManager] fileExistsAtPath:filename])
{
[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
}
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filename];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
self.food.text = nil;
self.movies.text = nil;
self.channel.text =nil;
NSLog(@"Info saved");
}
- (IBAction)viewInfo:(id)sender {
NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
NSString *filename = [docPath stringByAppendingString:@"result.csv"];
NSString *fileContent=[NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:nil];
resultView.text = fileContent;
}
請註明你所得到的異常,以及在什麼點你得到它 – ZeMoon