本文介绍了如何使用UITextView在Objective-C中为iPhone保存/加载文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将文本文件永久保存到iPhone?然后能够在另一个UITextView中再次打开它们吗?
How do you save text files permanently to an iPhone? And then be able to open them again in another UITextView?
提前致谢。
推荐答案
将NSString写入文件:
To write a NSString to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *str = @"hello world";
[str writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
从文件加载NSString:
To load NSString from a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
并使用UITextView的 text
属性设置并获取NSString。
And use text
property of UITextView to set and get NSString.
NSString *str = myTextView.text;
myAnotherTextView.text = str;
这篇关于如何使用UITextView在Objective-C中为iPhone保存/加载文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!