Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I migrate an xcassets catalog to Swift Package Manager?

I am migrating an app to Swift Package Manager. All my images are in an .xcassets, with their configurations for different display sizes etc.

How can I migrate those assets to SPM?

like image 418
Guig Avatar asked Jun 30 '20 02:06

Guig


People also ask

How do I add a Swift package to an existing project?

To add a new Swift package dependency: Select your project in the Project navigator, then select your app target and navigate to its General pane. Click the + button in the "Frameworks, Libraries, and Embedded Content" section, select the local package's library product, and add it as a dependency.

How do I add package manager to Swift?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

Can I include images in Swift Package Manager (SPM)?

I am migrating an app to Swift Package Manager. All my images are in an .xcassets, with their configurations for different display sizes etc. How can I migrate those assets to SPM? Show activity on this post. Starting with XCode 12 and Swift 5.3, it is possible to include images in Swift Package. I had to do a few modifications:

What is the Swift Package Manager in Xcode?

The idea of Swift Package Manager is that you should simply be able to go to: The native Swift Package Manager is built into Xcode and eliminates the need to install the package dependency manager using Homebrew, like you would for Carthage, or even having to run a simple “sudo gem install” command like you would for CocoaPods.

How to integrate Swift frameworks with Xcode?

A Swift package can contain multiple products (a product is a framework or an executable), you can select and integrate them individually in Xcode. Finally Xcode will do the required integration process, like embedding the library into the project and some other build configuration changes… Now you are ready to import your framework in your target.

How do I move the source catalog after migration?

Migration types are ‘copy’ and ‘move’. Select the’ move’ radio button if you wish to delete the source catalog after the migration is done. The default type is ‘copy’. Catalog folders, projects, and environments have explicit permissions.


1 Answers

Starting with XCode 12 and Swift 5.3, it is possible to include images in Swift Package. I had to do a few modifications:

  • Add // swift-tools-version:5.3 as the header of the Swift package file
  • Simply create a new asset catalog, or copy an existing one to the package hierarchy. .assets folder get compiled automatically.enter image description here
  • Make the image available in external targets by resolving the correct bundle. I went for this in the target containing the assets and an alternative would be to make each public asset available as a specific value.:
public func SCImage(named name: String) -> UIImage? {
  UIImage(named: name, in: Bundle.module, compatibleWith: nil)
}

I can now do

import SCImages

let image = SCImage(named: "foo")
like image 76
Guig Avatar answered Sep 18 '22 18:09

Guig