Copilot meets Xcode

Github Copilot for Swift iOS Developers

Some assembly required

Rudolf Farkas
3 min readMay 22, 2022

So, you signed up and joined the Github Copilot waiting list, waited a while, received the invitation, installed the Copilot extension in your VS Code, and started with co-programming.

Hand in hand with the Copilot you wrote scripts in JavaScript, Python, [your favorite language here], and even in Swift.

Amazed? Frustrated? Getting the hang of it? Enjoying the experience?

Probably all of the above.

But how about those iOS projects that you’re working on, in Xcode? Could your iOS or macOS projects benefit from the Copilot?

Currently the Copilot is available for Visual Studio Code, Visual Studio, Neovim, and JetBrains. Xcode is not listed, and there are no macrumors that I know of about a Copilot integration into Xcode.

But wait, let’s try something simple

  • open an iOS project in Xcode
  • open the VS Code at the Xcode project’s root folder
  • arrange both app windows side by side
  • open the same Swift file in both VS Code and Xcode
  • in VS Code start typing some Swift code or just write a Swift comment expressing your intent
  • the Copilot chimes in with some code in line (ctrl-enter opens a view on alternatives)
  • accept the most promising variant and save changes in VS Code
  • Xcode picks up the changes and updates its copy of the file
  • build and run the project in Xcode
  • it works! (well, probably not every time, it may need some nudging, retries and corrections).

Here is an example

I opened a new Xcode project and a new file, BackupFile.swift, and just inserted this comment

// a wrapper struct that backs up the wrapped struct in an iOS file

In the VS Code window (left) I accepted the code generated by the Copilot, saved it, and it rapidly percolated into the Xcode window (right):

VS Code side by side with Xcode

Here is the generated code:

Next I opened the file BackupFileTests.swift and declared my intent in a comment inside the func test_BackupFile():

// a sample struct for use in testing the BackupFile wrapper

From this comment the Copilot generated not only a sample struct but also the test code:

I ran the test in Xcode and it passed:

As an aside, the Copilot made a poor guess of my age, which is exactly twice the value it invented for me, in

let sample = Sample(name: “Rudolf”, age: 42)

Conclusions

Github Copilot is an excellent addition to a programmer’s toolbag.

With a bit of manual assembly you can use it today on your Xcode projects.

It does not do all the programming work for you, and often it needs nudging to go in the right direction. On the other hand, it picks up rapidly your stylistic preferences, after you typed some example code. It excels in generating variants, for example in writing unit tests. And it can save you much of the time that would be spent to look up minute details of the APIs and frameworks that you need for the task at hand.

Hints and tricks

You can install the Rectangle app to help arrange the VS Code and Xcode windows side by side.

You can add a shortcut to Xcode that will open VS Code in the Xcode project root folder. Here is how:

  • create a script named ~/.xcode/.xcode_open_vscode_here:
  • in Xcode > Behaviors > Edit Behaviors… > Custom link the script to a keyboard shortcut, for example ctrl-shift-cmd-C
Setting up an Xcode Custom Behavior (keyboard shortcut)

As you can see, I have several similar Xcode shortcut+script navigation and action combos.

Feedback

@rudifa

re-github-copilot @rudifa

--

--