Created
September 7, 2022 07:25
-
-
Save liuzhida33/b9b4edf625c275920c1929668a715f36 to your computer and use it in GitHub Desktop.
访问Bundle资源
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
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