[英]Bundle.main.path(forResource:ofType:inDirectory:) returns nil when directory and filename are correct
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3", inDirectory: "Songs")!
Returns nil. 返回nil。 songs[indexPath.row] returns the correct name, as it is exactly in the folder named "Songs".
songs [indexPath.row]返回正确的名称,因为它恰好位于名为“ Songs”的文件夹中。 The songs folder is blue and shows up correctly in my build phases->copy bundle resources
歌曲文件夹为蓝色,并在我的构建阶段->复制捆绑资源中正确显示
I replied in the comment above but I should go to this place to upload the image and recap my opinion again: 我在上面的评论中回答了,但我应该去这个地方上传图片并再次回顾一下我的观点:
make sure you removed the dot .
确保删除了点
.
: mp3
instead of .mp3
:
mp3
而不是.mp3
songs[indexPath.row]
value must not be optional, because it can cause a string like this: Optional(MyFileName)
, and the value must not contains the extension .mp3
, just filename only. songs[indexPath.row]
值不能是可选的,因为它可能导致这样的字符串: Optional(MyFileName)
,并且该值不能包含扩展名.mp3
,而只能包含文件名。
Look at the picture below, if you Bundle.main
equivalence is not a selected target in the list of File Inspector, the audioPath
may be nil
. 看下面的图片,如果
Bundle.main
等效项不是File Inspector列表中的选定目标,则audioPath
可能为nil
。 So you have to check them for all of your mp3 files. 因此,您必须检查它们的所有mp3文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.