I see that Apple developer use Bundle.module in Resources and localization session, but it doesn't seem to be available in Xcode 12. Is that an unreleased addition, or a private extension of some sort?
What is Bundle.module?
Okay, they explain it later in the video. Turns out it is synthesized accessor for frameworks' bundle. Super useful actually! No more
stuff
Code Block Bundle(for: Self.self)
stuff
Yah, this is automatically generated for any package target that includes at least one resource.
See also: https://github.com/apple/swift-package-manager/blob/master/Sources/Build/BuildPlan.swift#L577
See also: https://github.com/apple/swift-package-manager/blob/master/Sources/Build/BuildPlan.swift#L577
Hi,
you can also check out the developer docs for Swift packages:
you can also check out the developer docs for Swift packages:
When Swift Package Manager builds a target with resources, it automatically creates and adds a file to the module called resource_bundle_accessor.swift. This file's contents are roughly equivalent to:
Which adds a static module property to Foundation's Bundle type. The property is not public, so this static property exists only in the specific target that owns the bundle's resources—other modules will either have their own version of Bundle.module which returns their bundle, or won't have a Bundle.module property at all.
Since this property is created by adding a file to your target, it isn't technically part of Foundation, so it's not listed in the Foundation documentation. However, including it in the Foundation documentation anyway isn't a bad idea! Please use Feedback Assistant to file this suggestion.
Code Block swift import Foundation extension Bundle { static let module = Bundle(path: "\(Bundle.main.bundlePath)/path/to/this/targets/resource/bundle") }
Which adds a static module property to Foundation's Bundle type. The property is not public, so this static property exists only in the specific target that owns the bundle's resources—other modules will either have their own version of Bundle.module which returns their bundle, or won't have a Bundle.module property at all.
Since this property is created by adding a file to your target, it isn't technically part of Foundation, so it's not listed in the Foundation documentation. However, including it in the Foundation documentation anyway isn't a bad idea! Please use Feedback Assistant to file this suggestion.
I've found out that if you don't generate Xcode Project file for the package but open package folder with Xcode instead there are no errors with Bundle.module and you can see generated code for searching your module resources.
Nice! With this in mind though I still need the same functionality for devs integrating my framework using e.g. Carthage and there I still use Bundle(for: Self.self). My Question now is how to detect if I am integrated using SPM or not, i.e. if I have to use Bundle.module or the old cousin above?
You must to include at least one resource to the target on Package.swift.
Some like this:
In my example, I have a resource in a "Resources" folder inside "Sources/MyTestLibrary".
Once you put this resource on the Package on Build, Bundle.module appears like magic ;)
Best regards,
Some like this:
Code Block .target( name: "MyTestLibrary", dependencies: [], resources: [.process("Resources/EmpleadosData.json")] )
In my example, I have a resource in a "Resources" folder inside "Sources/MyTestLibrary".
Once you put this resource on the Package on Build, Bundle.module appears like magic ;)
Best regards,