我正在使用Swift包管理器创建一个macOS可执行文件。当我使用并非在所有macOS版本中都可用的东西时,我会得到编译错误。URL(fileURLWithPath: filePath, relativeTo: directoryToSearch)和url.hasDirectoryPath就是两个很大的例子。
当使用swift build构建时,我得到了error: 'init(fileURLWithPath:relativeTo:)' is only available on OS X 10.11 or newer错误。我不关心任何旧的操作系统版本,因为它只是一个个人工具。我如何才能将部署目标设置为10.14,这样我就不必在整个代码中都进行检查了?
我找到了关于这个问题的https://hirschmann.io/swift-package-manager/。但是,它的解决方案是创建一个设置了部署目标的xcconfig文件,并使用swift package generate-xcodeproj --xcconfig-overrides ./main.xcconfig将其应用于生成的Xcode项目。虽然它确实可以工作,但它只适用于Xcode项目,所以如果我只想做swift build来获得在Xcode外部使用的独立可执行文件,那么我就不能。
我的包文件是由swift package init --type executable自动生成的,并且没有更改。
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "swift_converter",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "swift_converter",
dependencies: []),
.testTarget(
name: "swift_converterTests",
dependencies: ["swift_converter"]),
]
)发布于 2019-02-16 01:05:48
这可能现在对你没有帮助,但即将到来的Swift 5.0将包括specify the deployment target in the package manifest的能力,使用如下语法:
...
platforms: [
.macOS(.v10_13), .iOS(.v12),
],
...(for some other common build settings也是如此。)
在此之前,您可以通过如下命令行参数覆盖默认部署目标:
$ swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.14"您必须在每次调用swift build、swift run、swift test时都包含这些参数。
https://stackoverflow.com/questions/54712375
复制相似问题