简体   繁体   English

当目录和文件名正确时,Bundle.main.path(forResource:ofType:inDirectory :)返回nil

[英]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.

相关问题 Bundle.main.path(forResource:ofType:inDirectory:) 返回 nil - Bundle.main.path(forResource:ofType:inDirectory:) returns nil 当应用名称包含“空格”时,Bundle.main.path(forResource:ofType:inDirectory :)返回nil - Bundle.main.path(forResource:ofType:inDirectory:) returns nil when app name contains “space” Bundle.main.path(forResource :)返回nil - Bundle.main.path(forResource:) returns nil 为什么Bundle.main.path(forResource:fileName,ofType:“ txt”)总是返回nil? - why does Bundle.main.path(forResource: fileName, ofType: “txt”) always return nil? URL(fileURLWithPath:Bundle.main.path(forResource:“ ports”,ofType:“ geojson”)!)是零吗? - URL(fileURLWithPath: Bundle.main.path(forResource: “ports”, ofType: “geojson”)!) is nil? 当使用 UIImage(named:“”) 和 Bundle.main.path(forResource:) 合成图像时,为什么性能会有很大差异? - When use UIImage(named:“”) and Bundle.main.path(forResource:) compositing images, why is the performance very different? -[NSBundle pathForResource:ofType:inDirectory:]对于本地化资源返回nil - -[NSBundle pathForResource:ofType:inDirectory:] returns nil for localized resources Bundle.main.url(forResource:“ hello”,withExtension:“ html”)返回nil - Bundle.main.url(forResource: “hello”, withExtension: “html”) is returning nil Bundle.main.url(forResource: "AppleIncRootCertificate", withExtension: "cer") == nil - Bundle.main.url(forResource: "AppleIncRootCertificate", withExtension: "cer") == nil 在应用程序启动时读取 Bundle.main.path 是否耗时? - Reading Bundle.main.path at the launching of the app is time consuming or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM