在哪里放置 iPhone UITextView 的文本文件 [英] Where to put text files for iPhone UITextView
问题描述
我有多个带有相应 .txt 文件的 UITextView.我正在用 NSString 读取它们
I have multiple UITextViews with corresponding .txt files. I'm reading them with NSString's
stringWithContentsOfFile
但我不知道我应该把文件放在哪里.如果我将它放在 Mac 上的/tmp/中,它可以在模拟器中运行,但当然不能在实际设备上运行.那么我应该把这些文件放在哪里,这样它们就可以在模拟器和实际设备上运行.
but I don't know the path where I should put my files. If I put it to /tmp/ on my Mac, it works in Simulator, but, of course, doesn't work on the actual device. So where should I put the files, so they'll work on both Simulator and actual Device.
推荐答案
将它们作为资源添加到您的项目中,您将能够使用路径从您的应用程序包中加载它们:
Add them as resources to your project and you'll be able to load them from your application bundle using path:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"filename"
ofType:@"txt"];
如果您想让这些文件可编辑,您可以将它们保存在应用程序沙箱中的 Documents 文件夹中.要获取其路径,您可以使用:
If you want to make these files editable, though, you can keep them in Documents folder in application sandbox. To get its path you can use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
所以在这种情况下的工作流程可能如下:
So workflow in this case may be the following:
- 检查文档文件夹中是否存在所需的文件.如果没有 - 从资源中复制它.
- 从文件中读取数据(从 Documents 文件夹)
- 如果需要,将更新的数据保存到文件(在 Documents 文件夹中)
这篇关于在哪里放置 iPhone UITextView 的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!