Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Created September 7, 2022 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzhida33/b9b4edf625c275920c1929668a715f36 to your computer and use it in GitHub Desktop.
Save liuzhida33/b9b4edf625c275920c1929668a715f36 to your computer and use it in GitHub Desktop.
访问Bundle资源
public let localBundle = Bundle.fixedModule
private final class CurrentBundleFinder {}
private let bundleNameIOS = "Backend_Backend"
private let bundleNameTests = "Backend_BackendTests"
extension Foundation.Bundle {
/// Returns the resource bundle associated with the current Swift module.
///
/// # Notes: #
/// 1. This is inspired by the `Bundle.module` declaration
static var fixedModule: Bundle = {
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: CurrentBundleFinder.self).resourceURL,
// For command-line tools.
Bundle.main.bundleURL,
// Bundle should be present here when running previews from a different package
// (this is the path to "…/Debug-iphonesimulator/").
Bundle(for: CurrentBundleFinder.self).resourceURL?
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),
Bundle(for: CurrentBundleFinder.self).resourceURL?
.deletingLastPathComponent().deletingLastPathComponent(),
]
for candidate in candidates {
let bundlePathiOS = candidate?.appendingPathComponent(bundleNameIOS + ".bundle")
let bundlePathTests = candidate?.appendingPathComponent(bundleNameTests + ".bundle")
if let bundle = candidate.flatMap(Bundle.init(url:)) {
return bundle
}else if let bundle = bundlePathiOS.flatMap(Bundle.init(url:)) {
return bundle
} else if let bundle = bundlePathTests.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle")
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment