Xcode swift中资源文件夹中音频文件的路径

时间:2016-01-12 14:15:45

标签: swift resources

在swift中如何获取已复制到Xcode资源文件夹中的测试音频文件的文件路径

我找到的代码没有找到文件。我是否正在使用NSSearchPathForDirectoriesInDomains查找正确的目录?任何帮助非常感谢!

        let name = "test.wav"
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
        let filePath = paths.stringByAppendingPathComponent(name)
        let checkValidation = NSFileManager.defaultManager()

        if (checkValidation.fileExistsAtPath(filePath)) {
            print("found .wav")
        } else {
            print("file not found")
        }

1 个答案:

答案 0 :(得分:1)

我刚发现这个有效。

let name = "test.wav"  
let soundPath = (NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(name)
let soundURL = NSURL.fileURLWithPath(soundPath)