Using an IDE other than Xcode for iOS development until recently was a rather lonely path. This however tends to change with recent developments in the Apple ecosystem. Swift runs officially on Linux, Windows, AWS lambda, docker, and other places where you wouldn’t expect to find a programming language made by Apple.

直到*近,使用Xcode以外的IDE进行iOS开发都是一条很孤独的道路。 但是,随着Apple生态系统的*新发展,这种趋势往往会改变。 Swift可以在Linux,Windows,AWS lambda,docker和其他您不希望找到Apple编写的编程语言的地方正式运行。

Apple also wants to enable Swift to be a first-class citizen in 3rd party IDEs providing error reporting and code auto-completion. For that reason, they are developing SourceKit-LSP with plugins ready for popular IDEs such as Visual Studio Code (aka VSCode).

苹果还希望让Swift成为第三方IDE中的一等公民,从而提供错误报告和代码自动完成功能。 出于这个原因,他们正在开发SourceKit-LSP ,其插件可用于流行的IDE,例如Visual Studio Code(又名VSCode)。

Why would you want to use an IDE other than Xcode? Well, Xcode is not available on Linux or Windows in the first place. But even when being on a Mac, VSCode offers several nice features among of them a killer one called Live Share that allows 2 or more developers to collaborate remotely on the same file with live code co-editing. You can think of it as Google Docs for code.

为什么要使用Xcode以外的IDE? 好吧,首先,Xcode在LinuxWindows上不可用。 但是,即使在Mac上,VSCode也提供了许多不错的功能,其中一个杀手级功能称为Live Share ,它允许2个或更多开发人员通过实时代码共同编辑在同一文件上进行远程协作。 您可以将其视为Google Docs代码。

In the days of post-Covid and home office this feature is a real life saver and makes a developer’s life so much easier either if you want to do pair programming or briefly explain and get assistance on a certain task.

在Covid发布后和家庭办公室时代,此功能可为您节省大量时间,如果您想进行配对编程或简要地解释并获得特定任务的帮助,则使开发人员的生活变得更加轻松。

If you try to use Swift in VSCode you will get something like that:

如果您尝试在VSCode中使用Swift,则会得到类似以下内容:

Auto-completion does not work as expected
自动补全无法按预期工作

The steps needed to enable VSCode for iOS development are basically 3:

启用VSCode进行iOS开发所需的步骤基本上是3:

  • Enable Swift language support启用S​​wift语言支持
  • Add iOS framework support添加iOS框架支持
  • Add “app” target support添加“应用”目标支持

Since this guide focuses on iOS and this is currently possible only with a Mac for the rest of the guide we will assume we are running on a Mac. Otherwise you can go as far a step 1.

由于本指南着重于iOS,因此本指南的其余部分目前仅适用于Mac,因此我们假定我们在Mac上运行。 否则,您可以继续执行步骤1。

启用S​​wift语言支持 (Enable Swift language support)

Make sure you have Xcode and VSCode installed. This is the easiest way to hit the ground running since sourcekit-lsp comes prebundled with Xcode. Open a terminal and run:

确保已安装Xcode和VSCode。 由于sourcekit-lsp与Xcode捆绑在一起,因此这是*简单的运行方法。 打开一个终端并运行:

$ xcrun sourcekit-lsp

Assuming that you don’t see any output this is normal and means it works as expected. Hit Ctrl+C to end the process.

假设您没有看到任何输出,这是正常现象,意味着它可以正常工作。 按Ctrl + C结束该过程。

构建SourceKit-LSP扩展 (Build the SourceKit-LSP extension)

Now it is time to build the SourceKit-LSP extension. There is an unofficial SourceKit-LSP — Unofficial CI build but this did not work for me. Building it from source is not that hard. Just make sure you have node.js installed and then run:

现在该构建SourceKit-LSP扩展了。 有一个非官方的SourceKit-LSP —非官方的CI版本,但这对我不起作用。 从源代码构建它并不难。 只要确保您已安装node.js ,然后运行:

$ git clone https://github.com/apple/sourcekit-lsp.git
$ cd sourcekit-lsp/Editors/vscode/
$ npm run createDevPackage
$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix

After that restart VSCode.

之后,重新启动VSCode。

Contrary to what you might read elsewhere you don’t need to edit the source code of the extension at all.

与您在其他地方可能会读到的内容相反,您根本不需要编辑扩展程序的源代码。

If you try to open a Swift file now you will get an error

如果您现在尝试打开Swift文件,将会收到错误消息

The plugin can’t locate SourceKit-LSP’s location
插件找不到SourceKit-LSP的位置

配置扩展 (Configure the extension)

You need to let the plugin know the location of the sourcekit-lsp executable. Open settings.json from VSCode using Cmd+Shift+P and enter “Preferences: Open Settings (JSON)”.

您需要让插件知道sourcekit-lsp可执行文件的位置 使用Cmd + Shift + P从VSCode打开settings.json ,然后输入“首选项:打开设置(JSON)”。

Insert this JSON as the last entry in the existing JSON. Remember to add a comma in the previous line.

将此JSON作为现有JSON中的*后一个条目插入。 请记住在前一行添加逗号。

"sourcekit-lsp.serverPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"

添加iOS框架支持 (Add iOS framework support)

What is we achieved so far is to enable Swift development but VSCode has no idea about iOS frameworks. If you try to import UIKit you will get an error.

到目前为止,我们要做的是启用Swift开发,但是VSCode对iOS框架一无所知。 如果尝试导入UIKit,将会收到错误消息。

The UIKit framework is not recognized
无法识别UIKit框架

Insert this JSON in settings.json. You might need to update the x86_64-apple-ios13.6-simulator line based on the current version that you have installed.

将此JSON插入settings.json中 。 您可能需要根据已安装的当前版本更新x86_64-apple-ios13.6-simulator行。

"sourcekit-lsp.serverArguments": ["-Xswiftc","-sdk","-Xswiftc","/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk","-Xswiftc","-target","-Xswiftc","x86_64-apple-ios13.6-simulator",],

This will work for Swift packages that reference iOS frameworks like UIKit.

这将适用于引用iOS框架(如UIKit)的Swift软件包。

添加“应用”目标支持 (Add “app” target support)

So far we added iOS support but only for Swift packages. What if you want to work on a typical app target. Trying to open the root folder gives this error:

到目前为止,我们添加了iOS支持,但仅支持Swift软件包。 如果您想处理典型的应用目标该怎么办。 尝试打开根文件夹会出现以下错误:

SourceKit-LSP is looking for a Swift package manifest file
SourceKit-LSP正在寻找Swift软件包清单文件

What if you want to work on an iOS app and not on a Swift Package? SourceKit-LSP always looks for a package.json in root folder assuming that it always deals with a Swift package. An iOS app is not a Swift package and for that reason we need to trick SourceKit-LSP by adding a dummy Package.swift. Add this file to the root of your project (same level as .xcodeproj)

如果您想在iOS应用而不是Swift包上工作怎么办? 假设SourceKit-LSP始终处理Swift包,则它总是在根文件夹中寻找package.json。 iOS应用程序不是Swift包,因此,我们需要通过添加虚拟Package.swift来欺骗SourceKit-LSP  将此文件添加到项目的根目录 (与.xcodeproj相同的级别)

// swift-tools-version:5.2
import PackageDescriptionlet packageName = "PROJECT_NAME" // <-- Change this to yourslet package = Package(
  name: "",
  // platforms: [.iOS("9.0")],
  products: [
    .library(name: packageName, targets: [packageName])
  ],
  targets: [
    .target(
      name: packageName,
      path: packageName
    )
  ]
)

Don’t forget to set packageName to your own one.

不要忘记将packageName设置为您自己的。

Double check the location of the file
仔细检查文件的位置

Restart VSCode one last time and voila! Proper UIKit auto-completion

*后一次重新启动VSCode,瞧! 正确的UIKit自动完成