objective-c 如何在我的应用程序本地文档目录中将 NSString 保存为 .txt 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2184372/
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
How do I save an NSString as a .txt file on my apps local documents directory?
提问by RexOnRoids
How do I save an NSString as a .txt file on my apps local documents directory (UTF-8)?
如何在我的应用程序本地文档目录 (UTF-8) 中将 NSString 保存为 .txt 文件?
回答by Vladimir
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSError *error;
BOOL succeed = [myString writeToFile:[documentsDirectory stringByAppendingPathComponent:@"myfile.txt"]
atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
}
回答by Craig
Something like this:
像这样的东西:
NSString *homeDirectory;
homeDirectory = NSHomeDirectory(); // Get app's home directory - you could check for a folder here too.
BOOL isWriteable = [[NSFileManager defaultManager] isWritableFileAtPath: homeDirectory]; //Check file path is writealbe
// You can now add a file name to your path and the create the initial empty file
[[NSFileManager defaultManager] createFileAtPath:newFilePath contents:nil attributes:nil];
// Then as a you have an NSString you could simple use the writeFile: method
NSString *yourStringOfData;
[yourStringOfData writeToFile: newFilePath atomically: YES];
回答by RaffAl
He is how to save NSString into Documents folder. Saving other types of data can be also realized that way.
他是如何将 NSString 保存到 Documents 文件夹中的。保存其他类型的数据也可以通过这种方式实现。
- (void)saveStringToDocuments:(NSString *)stringToSave {
NSString *documentsFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [NSString stringWithString:@"savedString.txt"];
NSString *path = [documentsFolder stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:path contents:[stringToSave dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}
回答by brynbodayle
I was using this method to save some base64 encoded image data to the disk. When opening the text file on my computer I kept having trouble reading the data because of some line breaks and returns being added automatically.
我使用这种方法将一些 base64 编码的图像数据保存到磁盘。在我的计算机上打开文本文件时,由于某些换行符和返回值被自动添加,我一直无法读取数据。
The following code fixes this issue:
下面的代码修复了这个问题:
myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
// write string to disk
回答by hafhadg3
you could use NSUserDefaults
你可以使用 NSUserDefaults
Saving:
保存:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
Reading:
读:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

