How to Use GitHub Copilot with Swift using VSCode?

GitHub copilot is available for a 60-day free trial so I signed up last week and started using it to play around with my projects written in Python and Javascript using VSCode and it works great.

However I also wanted to use GitHub Copilot to develop some IOS apps using Swift in XCode but unfortunately, GitHub Copilot is only available for Visual Studio Code, Visual Studio, Neovim, and JetBrains but not for XCode.

There is a nice attempt using Xcode Source Editor Extension in this GitHub project by “intitni” that allows you to run Github Copilot in Xcode but honestly, it is a bit clunky to use and not as seamless experience compared to VSCode.

Good thing there is a way to accomplish it in VSCode. Check out my step-by-step guide.

How To Setup VSCode for Swift Development

First, we need VSCode to show definitions and code completion when writing in the Swift language. To do this we need the Language Server Protocol(LSP) which is supported on both VSCode and Swift via a package called SourceKit-LSP.

Step 1: Get SourceKit-LSP by installing XCode

Having XCode 11.4+ and higher, the SourceKit-LSP server is included, no need for a separate download. Hopefully, you should already have XCode, if not, go to Mac App Store and download XCode. I’m using XCode 14.2 at the time of writing this post.

XCode 14.2

It can be run from the terminal with the command

xcrun sourcekit-lsp

I find that I don’t need to run this command explicitly for the SourceKit-LSP to work.

Step 2: Install Swift Extension in VSCode

Open VSCode and go to the Extension Marketplace and search for “Swift” then select from the results and go ahead and install.

Swift Extension for VSCode

Step 4: Install the GitHub Copilot extension in VSCode

You must have an active GitHub Copilot subscription for this to work. Open the Extension Marketplace and search for “GitHub Copilot” then select from the results and go ahead and install. You can follow this getting started guide.

Github Copilot extension for VSCode

Using GitHub Copilot With Swift Samples

There is a variety of ways GitHub Copilot could assist when writing code. It would attempt to suggest the next code. (Hit the tab key to accept the suggestion or hit the esc key).

Start creating the struct view by just typing the initial code:

struct MyView

Suggest a function by typing the name:

func add

Create code based on comments using natural language( like ChatGPT)

// create a view with a slider that adjusts the opacity

If you want more options and suggestions, hit CTRL + Enter and it would open the suggestions in a new tab, where you can accept solutions.

By the way, you can have XCode open on the same project as your VSCode Editor so you can preview using XCode Canvas. This way you can preview the changes and if it complies properly.

It seems VSCode won’t tell you it won’t compile. (eg. you have two structs of the same name)

There is another way to preview the changes that I haven’t figured out yet but it entails using xcodebuild command to run and launch the simulator. I’ll save that for another post.

Finally, you can also enable/disable GitHub copilot from the bottom toolbar anytime from the GitHub Copilot icon.

Summary

Overall the GitHub Copilot reduces the code that you would write, but you still need to know you would design your app, you can start with the initial codes and Copilot would complete it for you. It may not be perfect and you still need to tweak it to your liking. But it does speed up the development.

Check out my youtube channel for the video post.

Leave a Reply

Your email address will not be published. Required fields are marked *