且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift - 如何获取文件夹中的文件路径

更新时间:2023-01-15 19:03:03

首先确保将文件夹audioFiles拖到项目中以选择创建文件夹引用,它应该显示一个蓝色文件夹。

First make sure when you drag your folder audioFiles to your project to select create folder references and it should show a blue folder.

NSBundle方法pathForResource还有一个初始化程序,您可以在其中指定文件所在的目录:

Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:

let audioFileName = "audioName" 

if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
    print(audioFilePath)
}

如果您想获取该文件URL,可以使用NSBundle方法URLForResource(withExtension:,子目录:)

If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)

if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
    print(audioFileURL)
}