ios Swift - 如何获取文件夹内的文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34548771/
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
Swift - How do I get the file path inside a folder
提问by Rahul Krishna
I have a set of audio files inside a folder. I am able to access the file when the file is placed at main bundle, but if the files are moved inside the folder I am not able to access the files.
我在一个文件夹中有一组音频文件。当文件放置在主包中时,我可以访问该文件,但是如果将文件移动到文件夹内,我将无法访问这些文件。
Code:
代码:
let audioFileName:String = "audioFiles/" + String(index)
let audioFile = NSBundle.mainBundle().pathForResource(audioFileName, ofType: "mp3")!
I have an audio file inside the folder audioFiles and I would want to get its path.
我在文件夹 audioFiles 中有一个音频文件,我想获取它的路径。
Error:
错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
致命错误:在展开可选值时意外发现 nil
回答by Leo Dabus
First make sure when you drag your folder audioFiles to your project to select create folder references and it should show a blue folder.
首先确保当您将文件夹 audioFiles 拖动到您的项目以选择创建文件夹引用时,它应该显示一个蓝色文件夹。
Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:
NSBundle 方法 pathForResource 也有一个初始化程序,您可以指定文件所在的目录:
let audioFileName = "audioName"
if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
print(audioFilePath)
}
If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)
如果您想获取该文件的 URL,您可以使用 NSBundle 方法 URLForResource(withExtension:, subdirectory:)
if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
print(audioFileURL)
}
回答by Unome
The answer by Leo should work, the critical step is checking "Copy Items if Needed". However, what do you do if you already created a file or forgot that critical step?
Leo 的答案应该有效,关键步骤是检查“如果需要,请复制项目”。但是,如果您已经创建了一个文件或忘记了那个关键步骤,您会怎么做?
Its easy to fix.
它很容易修复。
Go to Project
-> Build Phases
-> Copy Bundle Resources
去Project
-> Build Phases
->Copy Bundle Resources
Here you will see a handy list of all the files you've added to your bundle (including all your xcassets!)
在这里,您将看到已添加到捆绑包中的所有文件的方便列表(包括所有 xcassets!)
Click the add button, and find your missing unlinked file.
单击添加按钮,找到丢失的未链接文件。
Your code will now work (I built my own example, so it won't be same as yours in name):
您的代码现在可以工作(我构建了自己的示例,因此它的名称与您的名称不同):
if let url = Bundle.main.url(forResource: "testData2", withExtension: "txt") {
do {
let myData = try Data(contentsOf: url)
print(myData.count)
} catch {
print(error)
}
}
So why weren't we finding the file to begin with? Simple. We were asking the bundle. "Hey where is my file". Bundle was like... I don't know about that file, and if you checked the list of what was part of it (the Bundle Resources that are being copied) it wasn't there. taDa!
那么为什么我们没有找到文件开始呢?简单的。我们问的是捆绑。“嘿,我的文件在哪里”。Bundle 就像......我不知道那个文件,如果你检查了它的一部分(正在复制的 Bundle 资源)的列表,它就不存在了。达!