Kyle
Kyle

Reputation: 17677

Package.swift - where do copied resources go?

Say I have a Package.swift with the following:

 targets: [
     .target(
         name: "SomeTarget",
         dependencies: [],
         path: "SomePath",
         resources: [.copy("../Assets")]
     )
 ]

And the Assets folder contains:

How can I access these files?

(There is a similar question here which references Bundle.module, but if I reference that, I get a Type 'Bundle' has no member 'module' error).

I've tried enumerating all the bundles searching for the xib:

    var targetBundle:Bundle
    let allBundles = Bundle.allBundles

    // Set as the first
    targetBundle = allBundles.first!

    for tempBundle in allBundles {
        if let path = tempBundle.path(forResource: "myFile", ofType: "xib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
        else if let path = tempBundle.path(forResource: "myFile", ofType: "nib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
    }

But it doesn't get found. If I open the .app package on the file system, I also cannot find the assets anywhere - I even extracted the Assets.car file in the main app bundle to see if they exist within that, but to no avail.

Where can I find these assets?

Upvotes: 4

Views: 5062

Answers (1)

Wode Liu
Wode Liu

Reputation: 433

When a swift package has resources, and the resources is put in the right directory(which is in the target folder under Sources folder), SPM will automatically create a resource_bundle_accessor.swift after building and add it to the module. The file adds a static module properly for Bundle, you can actually find the file in DerivedSources.
If you get the no module error, moving your resource folder to the right position will probably fix it. In your case, change resources in Package.swift to .copy("Assets"), and put Assets under Sources/SomeTarget.

Upvotes: 2

Related Questions