问题描述
我一直在使用Cocoapods管理Swift项目的依赖项.我遇到了此程序包,该程序未在Cocoapods上列出.相反,它建议使用Swift Package Manager.但是,每当我尝试使用Swift Package Manager基本执行任何操作时,它最终都会完全破坏我的整个项目.
I've been using Cocoapods to manage dependencies for my Swift project. I came across this package which is not listed at Cocoapods. Instead it suggests using Swift Package Manager. However, whenever I try to use Swift Package Manager to do basically anything, it ends up completely destroying my entire project.
因此,为了弄清楚如何实际使用Swift Package Manager,我在一个测试项目中对其进行了测试.
So, in order to figure out how to actually use Swift Package Manager, I'm playing with it in a test project.
这是我尝试过的:
- 从Xcode创建一个新项目
产品名称:basic-ssh-test
Product Name: basic-ssh-test
这将创建一个基本应用程序,当我单击运行"时,该应用程序将加载空白窗口.只是为了好玩,我在AppDelegate
的applicationDidFinishLaunching
函数中添加了print("test")
,因此在运行程序时,调试窗口会显示测试".
This creates a basic application which loads a blank window when I hit "Run". Just for fun I added print("test")
to the applicationDidFinishLaunching
function in my AppDelegate
so the debug window says "test" when I run the program.
现在,我想添加Shout
软件包作为依赖项.
Now I want to add the Shout
package as a dependency.
-
swift package init --type executable
这将创建以下文件:
Creating executable package: basic-ssh-test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/basic-ssh-test/main.swift
Creating Tests/
现在,我将"Shout"依赖项添加到新的Package.swift
文件中:
Now I'm going to add the "Shout" dependency to my new Package.swift
file:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "basic-ssh-test",
dependencies: [
.package(url: "https://github.com/jakeheis/Shout", from: "0.2.0")
],
targets: [
.target(
name: "basic-ssh-test",
dependencies: ["Shout"]),
]
)
然后我将介绍依赖项:
-
swift package resolve
这导致依赖项被拉入.build
目录:
This results in the dependencies being pulled into the .build
directory:
Fetching https://github.com/jakeheis/Shout
Fetching https://github.com/jakeheis/CSSH
Fetching https://github.com/IBM-Swift/BlueSocket
Cloning https://github.com/IBM-Swift/BlueSocket
Resolving https://github.com/IBM-Swift/BlueSocket at 0.12.91
Cloning https://github.com/jakeheis/CSSH
Resolving https://github.com/jakeheis/CSSH at 1.0.3
Cloning https://github.com/jakeheis/Shout
Resolving https://github.com/jakeheis/Shout at 0.3.0
现在,我重新生成xcodeproj
文件:
Now I regenerate the xcodeproj
file:
-
swift package generate-xcodeproj
现在,当我打开xcodeproj
文件时,有一个名为Sources
的新组,该组具有一个目录basic-ssh-test
和一个单独的快速文件,其中的main.swift
和print("Hello, world!")
.
Now when I open the xcodeproj
file there is a new group called Sources
that has a single directory basic-ssh-test
with a single swift file in it main.swift
with print("Hello, world!")
.
我实际上对运行感兴趣的代码现在位于名为basic-ssh-test
的蓝色文件夹中.所有必需的文件仍在其中,但是Xcode不在运行我的应用程序,而是在运行main.swift
文件.我可以知道,因为调试输出是"Hello,world!".而不是测试".
The code that I'm actually interested in running is now in a blue folder called basic-ssh-test
. All of the necessary files are still in there, but instead of running my application, Xcode is running the main.swift
file. I can tell because the debug output is "Hello, world!" instead of "test".
我已经阅读了几篇教程,其中声称Swift Package Manager会移动我的源文件并继续像以前一样构建,但是显然不是这种情况.
I've read a couple of tutorials that claim that Swift Package Manager will move my source files and continue to build the same as before, but that's clearly not the case.
构建设置"中也不再包含主界面"选项,因此无法选择"MainMenu.xib"作为我的应用程序起点.
There's also no longer a "Main Interface" option in my Build Settings, so I can't select "MainMenu.xib" as my application starting point.
当我尝试对现有项目使用Swift Project Manager时,这基本上是相同的事情.它引入了依赖关系,但基本上会忽略我现有的整个项目,而只运行"hello world"代码.
This is essentially the same thing that happens when I try to use Swift Project Manager with my existing project. It pulls in the dependencies, but it basically ignores my entire existing project and just runs the "hello world" code.
如何使用Swift Package Manager在不忽略现有代码库的情况下向现有项目添加依赖项?
How do I use Swift Package Manager to add a dependency to an existing project without ignoring the existing codebase?
推荐答案
我认为SPM仅与Mac命令行可执行文件或库兼容. 此明确声明SPM根本不支持iOS,watchOS或tvOS平台.但是,由于macOS AppKit/Cocoa应用程序目标与iOS或tvOS Xcode目标非常相似,我想说这句话意味着SPM也不能与macOS Cocoa应用程序一起使用,这就是我认为的希望.
I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
似乎有此处有一些工作它与iOS应用程序目标结合在一起,而目标目标应很大程度上转化为macOS Cocoa目标.
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.
这篇关于如何将Swift Package Manager与现有的macOS项目一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!