Swift Package Manager — Integrating resources

Declaring resources

Resources can be bundled with a Swift package and then be accessed in your code.

targets: [
.target(
name: "MyLibrary",
resources: [
.process("Mocks")]
),
]

Accessing resources

If a target includes resources, Xcode creates a resource bundle and an internal static extension on Bundle to access it for each module.

extension Bundle {
func dataFromResource(_ resource: String) -> Data {
guard let mockURL = url(forResource: resource,
withExtension: "json"),
let data = try? Data(contentsOf: mockURL) else {
fatalError("Failed to load \(resource) from bundle.")
}
return data
}
}
Bundle.module.dataFromResource("mockName")
public let someData = Bundle.module.dataFromResource(forResource: "mockFileName")

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store