Carter
Carter

Reputation: 1

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. songs[indexPath.row] returns the correct name, as it is exactly in the folder named "Songs". The songs folder is blue and shows up correctly in my build phases->copy bundle resources

Upvotes: 0

Views: 516

Answers (1)

ngbaanh
ngbaanh

Reputation: 535

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

  • 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.

  • 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. So you have to check them for all of your mp3 files.

enter image description here

Upvotes: 2

Related Questions