如何将iphone应用程序中的数据保存到文本文件并保存在文档中
问题描述:
因为我们知道可以在iphone应用程序中创建pdf文件,但我希望我的数据在表格视图中必须保存在文档中在一个文本文件中的应用程序。如何将iphone应用程序中的数据保存到文本文件并保存在文档中
答
您创建一个文本文件,让它成为“file.txt的”并写入所需的数据,这里的“yourString”如下,
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *stringWithoutSpaces = [yourString stringByReplacingOccurrencesOfString:@" " withString:@""];
[stringWithoutSpaces writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
可以读取保存的数据如下,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *yourString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
可能的重复[写一个字符串到使用Xcode的iPhone开发文本文件](http://stackoverflow.com/questions/4129576/writing-a-string-to-a-text-file-using-xcode-for- iphone-dev的) – Ilanchezhian 2012-07-05 10:44:38