在IOS项目中何处放置.txt文件并从中读取 [英] Where to place a .txt file and read from it in a IOS project
问题描述
我想在我的Xcode Swift Iphone项目中放入一个.txt文件. 首先,我只是将其从桌面拖放到项目中的Supporting Files文件夹中.是否没有像Android资产"上的文件夹,所以我可以将文件放在我想要的任何位置?
i want to put a .txt file in my Xcode Swift Iphone project. First, i simply drag&dropped it from my desktop to the Supporting Files Folder from the project. Isn't there a Folder like on Android "assets", so i can place my files anywhere i want?
在我的示例中,文件名为README.txt,其中包含许多行和段落.
The file in my example is called README.txt which has a bunch of lines and paragraphs.
很简单,现在我想将README.txt文件的内容打印到视图中.
Simple enough, now I want to print the content of the README.txt file to a view.
如果我的文件位于项目/SupportFiles/README.txt中,我该如何执行读取功能以及应该插入什么路径?
How do i do the read function and what path should I insert, if my file is in the project /SupportFiles/README.txt?
非常感谢!
推荐答案
if let path = Bundle.main.path(forResource: "README", ofType: "txt") {
do {
textView.text = try String(contentsOfFile: path, encoding: .utf8)
} catch let error {
// Handle error here
}
}
只需将文件拖放到项目浏览器中的任何位置,并确保将其添加到正确的目标即可.
Just drop the file anywhere into the project browser and make sure it is added to the right target.
Just to expand on the answer, you can also place them in a folder and use: + pathForResource:ofType:inDirectory:.
这篇关于在IOS项目中何处放置.txt文件并从中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!