资源明明在,但是pathForResouces却返回空

今天拖了一个声音文件到项目中


资源明明在,但是pathForResouces却返回空_第1张图片

然后通过以下代码加载

func playAlertSound() {
        guard let soundPath = Bundle.main.path(forResource: "noticeMusic", ofType: "caf") else { return }
        guard let soundUrl = NSURL(string: soundPath) else { return }
        var soundID: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundUrl, &soundID)
        AudioServicesPlaySystemSound(soundID)
    }

但是第一句的时候soundPath为空就直接返回了.挠头半天也找不到原因, 后来还是stackoverflow帮忙解决了,如下图


资源明明在,但是pathForResouces却返回空_第2张图片
Snip20161207_3.png

在CopyBundleResources里把声音文件加进去就好啦.

嗯,这个声音格外好听.

原文链接

你可能感兴趣的:(资源明明在,但是pathForResouces却返回空)