Bundle.main.url returning nil

Hi, I have added an audio asset into my .swiftpm file in a folder called “Audios”, and have tried getting it by the following methods

  1. Bundle.main.url(forResource: "Completed", withExtension: "mp3")
  2. Bundle.main.url(forResource: "Completed", withExtension: "mp3", subdirectory: "AppData")

However, it still returns nil for both methods. I have also tried deleting and adding the file back but to no avail.

I would greatly appreciate any help! Thanks.

Accepted Answer

When adding resources to a Swift Package, you need to configure the manifest, to explicitly declare the resources.
Have you done this?

Perhaps something like...

    targets: [
    .target(
        name: "MyLibrary",
        resources: [
            .process("Audios/Completed.mp3")]
    ),
]

Then you can refer to the resources in code.

See: https://developer.apple.com/documentation/swift_packages/bundling_resources_with_a_swift_package

Bundle.main.url returning nil
 
 
Q