Created
June 2, 2022 08:05
resource_bundle_accessor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import class Foundation.Bundle | |
private class BundleFinder {} | |
extension Foundation.Bundle { | |
/// Returns the resource bundle associated with the current Swift module. | |
static var module: Bundle = { | |
let bundleName = "<PACKAGENAME_MODULENAME>" | |
let candidates = [ | |
// Bundle should be present here when the package is linked into an App. | |
Bundle.main.resourceURL, | |
// Bundle should be present here when the package is linked into a framework. | |
Bundle(for: BundleFinder.self).resourceURL, | |
// For command-line tools. | |
Bundle.main.bundleURL, | |
] | |
for candidate in candidates { | |
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") | |
if let bundle = bundlePath.flatMap(Bundle.init(url:)) { | |
return bundle | |
} | |
} | |
fatalError("unable to find bundle named <PACKAGENAME_MODULENAME>") | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment