xcode 在 iPhone 上写入和读取文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4964274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing and reading text files on the iPhone
提问by tallen11
As a practice, I am trying to write an app similar to the built-in notes app.
But I cannot figure out how to save the fileand display it in a UITableView
.
Right now, I have a UITextView
that the user can type in. I also have a savebutton.
When the user taps the save button, I want to save it, and later have it displayed in a table view.
I am very lost so if you know of any relevant tutorials etc. it would be greatly appreciated.
作为一种实践,我正在尝试编写一个类似于内置笔记应用程序的应用程序。
但我不知道如何保存文件并将其显示在UITableView
.
现在,我有一个UITextView
用户可以输入的内容。我还有一个保存按钮。
当用户点击保存按钮时,我想保存它,然后将它显示在表格视图中。
我很迷茫,所以如果你知道任何相关的教程等,将不胜感激。
回答by Matt Wilding
As noted by the commenters in the real world, you're definitely going to want to look at Core Data or some other data persistence strategy. If you're dead set on pursuing this as a learning experience, something like this should solve your problem:
正如现实世界中的评论者所指出的,您肯定会想要查看 Core Data 或其他一些数据持久性策略。如果您决心将其作为一种学习体验来追求,那么这样的事情应该可以解决您的问题:
- (void)writeStringToFile:(NSString*)aString {
// Build the path, and create if needed.
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"myTextFile.txt";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
}
// The main act...
[[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}
- (NSString*)readStringFromFile {
// Build the path...
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"myTextFile.txt";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
// The main act...
return [[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding] autorelease];
}
回答by Ray Lillywhite
The easiest way to save text is using NSUserDefaults.
保存文本的最简单方法是使用 NSUserDefaults。
[[NSUserDefaults standardUserDefaults] setObject:theText forKey:@"SavedTextKey"];
or, if you want to have the user name each "file" or be able to have multiple files
或者,如果您希望每个“文件”都有用户名或能够拥有多个文件
NSMutableDictionary *saveTextDict = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedTextKey"] mutableCopy] autorelease];
if (saveTextDict == nil) {
saveTextDict = [NSMutableDictionary dictionary];
}
[saveTextDict setObject:theText forKey:fileName];
[[NSUserDefaults standardUserDefaults] setObject:saveTextDict forKey:@SavedTextKey"];