Swift软件包管理器-捆绑包旁边的Bundle.module(用于:)

Swift软件包管理器-捆绑包旁边的Bundle.module(用于:),swift,frameworks,resources,swift-package-manager,Swift,Frameworks,Resources,Swift Package Manager,我有一个外部库,在那里我可以访问嵌入的资源。该库应该可以通过SPM和框架(例如直接链接、Carthage或Cocoapods)访问 问题是要访问这些资源,当库用作swift包时,我必须调用Bundle.module,当库用作框架时,我必须调用Bundle(for:) 有没有办法检查我是将库编译为swift包还是框架,比如预处理器条件 编辑:这个问题需要更加集中。 怎么做?我解释我的问题并问一个问题?无法更加集中。捆绑包。无论您的包是否包含任何资源,模块将与内部访问级别合成。只有在打开包.swif

我有一个外部库,在那里我可以访问嵌入的资源。该库应该可以通过SPM和框架(例如直接链接、Carthage或Cocoapods)访问

问题是要访问这些资源,当库用作swift包时,我必须调用
Bundle.module
,当库用作框架时,我必须调用
Bundle(for:)

有没有办法检查我是将库编译为swift包还是框架,比如预处理器条件

编辑:
这个问题需要更加集中。


怎么做?我解释我的问题并问一个问题?无法更加集中。

捆绑包。无论您的包是否包含任何资源,模块将与内部访问级别合成。只有在打开
包.swift
清单文件时,它才可见,尝试以其他方式访问它会导致错误。 Swift软件包管理器不依赖于
.xcodeproj
,因此您只需开始即可

Cocoapods,迦太基没有这样的优势,所以你必须合成你自己的

//
//  Bundle+Resource.swift
//  DateTimePicker
//
//  Created by Duy Tran on 10/9/20.
//

import Foundation

private class MyBundleFinder {}

extension Foundation.Bundle {
    
    /**
     The resource bundle associated with the current module..
     - important: When `DateTimePicker` is distributed via Swift Package Manager, it will be synthesized automatically in the name of `Bundle.module`.
     */
    static var resource: Bundle = {
        let moduleName = "DateTimePicker"
        #if COCOAPODS
        let bundleName = moduleName
        #else
        let bundleName = "\(moduleName)_\(moduleName)"
        #endif
        
        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: MyBundleFinder.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 \(bundleName)")
    }()
}

似乎很多开发人员都在寻找解决方案,我将发表一篇中间文章寻求帮助。

您可以查看此合并请求,以便更仔细地查看