您如何使用 SPM 构建 iOS 个特定的软件包?

How do you build iOS specific packages using SPM?

我正在使用 Swift 包管理器,它没有与之关联的 xcodeproject 文件,并且在通过终端构建时出现错误。当我调用 swift build 命令时,出现 MacOS 构建失败的错误。我构建的包不支持 MacOS(它使用 UIKit),但只支持 iOS。我想不出一种方法来调用命令以仅指定构建针对 iOS。我已经 Google 搜索了一圈,但没有成功。有人知道从终端为 iOS 构建 SPM 包的正确语法吗?

我使用的Swift版本是:"Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)"

我已经在 Package.swift 文件中指定了平台

let package = Package(
    name: "Package",
    platforms: [.iOS(.v10), ],
    products: [
...

我在 github https://github.com/mike011/Swift-Package-Manager-Example 上创建了一个示例项目。当我 运行 swift build 它失败并显示

/git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^ /git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^

SwiftPM 目前没有办法禁止为特定平台构建,但如果您愿意,可以利用最低构建版本在您不支持的平台上导致编译时错误。

例如,如果您不想允许在 macOS 上构建,您可以使用平台版本:.macOS("99.0") 在清单的 platforms 部分,您将收到编译警告和在 Xcode:

中构建时出现与此类似的错误
The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 99.0, but the range of supported deployment target versions is 10.8 to 10.16.99
Invalid Darwin version number: macos99.0
Invalid version number in 'target x86_64-apple-macos99.0'

我通过将所有文件包装在 #if !os(macOS) #endif 块中“解决”了这个问题。所以这个包建立在 Mac 上,但它没有任何内容。