LoginSignup
9
12

More than 3 years have passed since last update.

UIKitに依存するSwift PackageをVSCodeで開発する

Last updated at Posted at 2020-07-07

XcodeがクソすぎてVSCodeが好きすぎてSwiftのコードを書くのにもVSCodeを使いたい!
と思って調べてみたところ、どうやらSwift PackageのプロジェクトならVSCodeでもそれなりにコード補完できるようになる、ということがわかりました。

こちらにあるのがApple公式のsourcekit-lsp(Swiftの構文補完のためのLanguage Server)リポジトリで、中を見るとVSCode用の拡張機能のコードもあるみたい。

なので、まずはそちらのREADMEに従ってVSCodeのsourcekit-lsp拡張機能をインストールします。上記のsourcekit-lspのリポジトリをクローンしてきて、

$ cd Editors/vscode
$ npm run createDevPackage
$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix

とするだけです!

※ Xcodeのインストールが必要です。
sourcekit-lspコマンドが実行できるようになっている必要があります。最近のXcodeなら入ってる…?もしくは上記リポジトリからビルドする。
※ npm コマンドを使うために、Node.jsのインストールが必要です。
※ code コマンドを使うために、VSCodeでCmd+Shift+PからInstall code command in PATHを実行する必要があります。

ではさっそくプロジェクトを作ります。

swift package init

プロジェクトができたら、さっそくコードを書いてみましょう。Sources/my-swift-ios(作ったディレクトリにより異なる)の中にMyViewController.swiftというファイルを作って次のようなコードを書きます。

import UIKit

…おや、import UIKitのところに赤い線が引かれて no such module UIKit と言われていますね。

そしたらVSCodeの設定画面を開き(Cmd+,)、右上の「設定(JSON)を開く」アイコンを押してsettings.jsonを開きます。
そして、その中に以下のような指定を加えます。

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

iPhoneSimulator13.5とかios13.5-simulatorとかいう部分はXcodeのバージョンにより正しい指定が異なるので、実際に/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKsの場所をFinderで開いて番号を確認してください。

設定ができたら、VSCodeを再起動(もしくはCmd+Shift+P→Reload Window)します。

改めて先程のswiftファイルを開いてみましょう。赤い線が消えていると思います(消えていない場合、上記の-Xswiftcで渡しているパスが正しいかどうか確認してください)。

output.gif

コード補完できてる〜!!!!

ちなみに、先程指定したsourcekit-lsp.serverArgumentsの設定は、swift buildをするときにも同様に必要になります。
なので、.vscode/tasks.jsonの中にこんな感じで指定すると良いでしょう。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "swift",
            "args": [
                "build",
                "-Xswiftc",
                "-sdk",
                "-Xswiftc",
                "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk",
                "-Xswiftc",
                "-target",
                "-Xswiftc",
                "x86_64-apple-ios13.5-simulator",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

これでVSCodeのビルド(Cmd+Shift+B)をするだけでビルドできるようになります。

テスト

ここまで書いて気付いたのですが、テスト(swift test)が動かない…orz

.vscode/tasks.jsonに以下のテスト実行の追記をしてみましたが…

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "swift",
            "args": [
                "build",
                "-Xswiftc",
                "-sdk",
                "-Xswiftc",
                "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk",
                "-Xswiftc",
                "-target",
                "-Xswiftc",
                "x86_64-apple-ios13.5-simulator",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "test",
            "type": "shell",
            "command": "swift",
            "args": [
                "test",
                "-Xswiftc",
                "-sdk",
                "-Xswiftc",
                "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk",
                "-Xswiftc",
                "-target",
                "-Xswiftc",
                "x86_64-apple-ios13.5-simulator",
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

error: module 'XCTest' was created for incompatible target と言われて実行できない。

2020/07/09追記:
ですが、xcodebuildコマンドを使うことでiPhone Simulator上でテストが可能であるということがわかりました!

.vscode/tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "swift",
            "args": [
                "build",
                "-Xswiftc",
                "-sdk",
                "-Xswiftc",
                "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk",
                "-Xswiftc",
                "-target",
                "-Xswiftc",
                "x86_64-apple-ios13.5-simulator",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "test",
            "type": "shell",
            "command": "xcodebuild",
            "args": [
                "-scheme",
                "my-swift-ios",
                "test",
                "-destination",
                "name=iPhone 8"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

これでiOSのコードがVSCodeでもそれなりに開発できる…かも!?!?

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12