Bundle module Conditions for generating
Posted: Sat Jan 18, 2025 8:33 am
Sorry for the wait, but here we finally get to the main topic.
Up until now
, it was assumed that you would create XCAssets within the module. But what if you want to place an image resource directly without using XCAssets and have SwiftPM load it?
To get straight to the point, if you place an image resource directly without using XCAssets, it Bundle.bundlewill not be generated automatically . This is
because there are certain files that will be detected and automatically generated .
XIB files or storyboards
xcdatamodeld (CoreData)
Asset catalogs (images, color)
.lproj (localization file xcstrings)
hydrangea.jpgPlease note that simply placing a togo email address resource file like this will not automatically detect it in Xcode due to the specifications of SwiftPM.
So what do we do?
The Apple documentation explains it like this:
To add a resource that Xcode can't handle automatically, explicitly declare it as a resource in your package manifest.
In other words, if you want to load a resource file that is not automatically detected, you must clearly state the path to the resource in Package.swift (package manifest).
It seems that the resource will only be imported once you specify the path to the resource for the target, and Bundle.module will also be generated from there.
resources:Let's start by specifying the path to the image resource for the FeatureA target .
In this case, we will specify the relative path from the target directory (FeatureA folder) to the path destination.
targets : [
.target (
name: "FeatureA" ,
resources: [
.process ( "./hydrangea.jpg" ) // Add the path to the resource!
] ) , ]
hydrangea.jpgNow you can reference the image resource from the source code in the FeatureA target .
Also, Bundle.modulesince you have access to ,
you can get the resource path directly from the source code.
hydrangea.jpgYou can also get the absolute path to the .
Since we can obtain the path to a specific file in this way, we can read not only image files like in this case, but also sound, video, animation files, and even JSON and our own custom bundle files.
Up until now
, it was assumed that you would create XCAssets within the module. But what if you want to place an image resource directly without using XCAssets and have SwiftPM load it?
To get straight to the point, if you place an image resource directly without using XCAssets, it Bundle.bundlewill not be generated automatically . This is
because there are certain files that will be detected and automatically generated .
XIB files or storyboards
xcdatamodeld (CoreData)
Asset catalogs (images, color)
.lproj (localization file xcstrings)
hydrangea.jpgPlease note that simply placing a togo email address resource file like this will not automatically detect it in Xcode due to the specifications of SwiftPM.
So what do we do?
The Apple documentation explains it like this:
To add a resource that Xcode can't handle automatically, explicitly declare it as a resource in your package manifest.
In other words, if you want to load a resource file that is not automatically detected, you must clearly state the path to the resource in Package.swift (package manifest).
It seems that the resource will only be imported once you specify the path to the resource for the target, and Bundle.module will also be generated from there.
resources:Let's start by specifying the path to the image resource for the FeatureA target .
In this case, we will specify the relative path from the target directory (FeatureA folder) to the path destination.
targets : [
.target (
name: "FeatureA" ,
resources: [
.process ( "./hydrangea.jpg" ) // Add the path to the resource!
] ) , ]
hydrangea.jpgNow you can reference the image resource from the source code in the FeatureA target .
Also, Bundle.modulesince you have access to ,
you can get the resource path directly from the source code.
hydrangea.jpgYou can also get the absolute path to the .
Since we can obtain the path to a specific file in this way, we can read not only image files like in this case, but also sound, video, animation files, and even JSON and our own custom bundle files.