Better Programming

Advice for programmers.

Member-only story

How to Add Resources in Swift Package Manager

5 min readMay 22, 2020

--

Photo by Ryo Yoshitake on Unsplash

SE-0271 by Anders Bertelrud and Ankit Aggarwal brings Resources to Swift Package Manager. If you have any iOS development experience, you’ll know that you can add non-compiled files with Bundle. In the new Swift Package Manager, based on Swift 5.3 or newer, you can add the same thing. You can add images, sound, and JSON and Swift will generate the bundle for you.

At the time of writing, Swift 5.3 isn’t officially released, but if you want to try it, download a snapshot build at Swift Snapshot.

On each target, know that you can declare recursively or specifically what resources you want to add — more details on this later.

.target(name: "HelloWorldProgram", dependencies: [], resources: nil)

If you add a non-compiled file to your target directory, Swift will give you this warning:

error: found 2 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
/Users/wendyliga/resource-spm/Sources/resource-spm/README.md
/Users/wendyliga/resource-spm/Sources/resource-spm/Images/image.png

To solve this, Swift 5.3 introduces resources parameters on .target. There are two resources you can define.

--

--

Wendy Liga
Wendy Liga

Written by Wendy Liga

Learning Driven Life • iOS Software Writer at Tokopedia • Exploring Swift and Anything that Sounds Fun • Open Source Enthusiast