Foundation.Bundle

extension Foundation.Bundle
  • module From SwiftlyChordUtilities

    Returns the resource bundle associated with the current Swift module.

    Declaration

    Swift

    static let module: Bundle = {
        let bundleName = "SwiftlyChordUtilities_SwiftlyChordUtilities"
    
        let overrides: [URL]
        #if DEBUG
        // The 'PACKAGE_RESOURCE_BUNDLE_PATH' name is preferred since the expected value is a path. The
        // check for 'PACKAGE_RESOURCE_BUNDLE_URL' will be removed when all clients have switched over.
        // This removal is tracked by rdar://107766372.
        if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"]
                       ?? ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_URL"] {
            overrides = [URL(fileURLWithPath: override)]
        } else {
            overrides = []
        }
        #else
        overrides = []
        #endif
    
        let candidates = overrides + [
            // 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 SwiftlyChordUtilities_SwiftlyChordUtilities")
    }()
  • Decode a JSON file from the bundle

    Declaration

    Swift

    func decode<T: Decodable>(
        _ type: T.Type,
        from file: String,
        dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate,
        keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys
    ) -> T

    Parameters

    type

    The decodable type

    file

    The fil to decode

    dateDecodingStrategy

    The optional Date Decoding Strategy

    keyDecodingStrategy

    The optional Key Decoding Strategy

    Return Value

    The decoded file

  • json(from:) From SwiftlyChordUtilities

    Get the JSON data from a file in the bundle

    Declaration

    Swift

    func json(from file: String) -> String

    Parameters

    file

    The JSON file

    Return Value

    The content of the JSON file