ChatGPT integration in Xcode: How to improve your Apps with AI

Adolfo
Globant
Published in
6 min readApr 13, 2023
Credits: Foto de DeepMind en Unsplash

The latest enfant terrible in the field of Artificial Intelligence is called ChatGPT and comes from OpenAI, a company founded a few years ago that is currently leading the way with products like the aforementioned ChatGPT or DALL-E.

But these products are not exempt from controversy due to the debate that their use has caused within software development and the possible disappearance of developers at the hands of ChatGPT or one of its older brothers that is yet to come.

To this day it has been seen that ChatGPT cannot replace a human developer, but we have verified that it can be a very useful tool in the programmer’s day-to-day.

We first need to integrate it into our favorite IDE, which is none other than Xcode, to avoid having to switch between the IDE and Safari to interact with ChatGPT.

For this, we have developed an Xcode Source Editor Extension that provides a solution to three specific problems:

  1. Code conversion. A command that converts a JSON document to a Swift struct.
  2. Code Smells. We ask ChatGPT to try to identify things that can be improved in our source code
  3. Explain the code. In the latter case, we are interested in knowing what a certain code does.

Working with ChatGPT

To interact with this model, we need to be registered on the OpenAI website and create an API key that we will pass in the HTTP headers when consuming the REST API.

You must save this API key as it will only be readable at the creation time.

Once we have created the API, we focus on the ChatGPT endpoint where the way in which we must pass the information to the API and the format in which it returns it to us is detailed.

To create this API key, we must follow the following steps

  1. Access our user profile in OpenAI
  2. Select the API Keys option in the side menu, and once there click on the Create new secret key button
OpenAI developer API key generation

In the request that we send to the API, the information goes in the body of the HTTP request, in addition to indicating the model with which we want to work.

The prompt of the request is indicated in the content property, and within the project, it is found in a localization file in the target of the extension, since ChatGPT can understand us if we ask it in English or Spanish.

Our API key is passed as an HTTP header in each request.

Creating the Xcode extension

We must first create a new project for macOS; once this is done, we must add a new target of the type Xcode Source Editor Extension.

Xcode Source Editor Extension target type

Once the extension is created, we must change the Frameworks and Libraries section of the General tab of the new target.

Once there, we change the Embed property of the XcodeKit framework to Embed & Sign since if we leave it as Do Not Embed, an error will occur when executing the extension, and we will not see it in Xcode.

Xcode target framework and libraries: Embed & Sign the XodeKit framework

When we create an extension of the Xcode Source Editor type, two files are created

1. SourceEditorExtension This class implements the XCSourceEditorExtension protocol and allows us to execute code when the extension is finished loading and also a property to define the different commands that our extension has.

2. SourceCodeCommand. Contains our extension. Implements the XCSourceEditorCommand protocol.

Creating a command

All commands in an Xcode extension must implement the XCSourceEditorCommand protocol, which defines a function called perform.

Said function receives as a parameter an object of type XCSourceEditorCommandInvocation and a closure where we will report the result of the execution of our extension.

The parameter called invocation is the one that allows us to access the content of the source code file that we have open at that moment, thanks to the property buffer.

This buffer property allows us to access the entire file content thanks to the completeBuffer property or all the content in array format line by line through the lines property, which is of the NSMutableArray type.

Thanks to these two properties we can read and modify the file's content according to our needs.

But to make our commands visible, we must add them to the info.plist file of the extension using a dictionary that has three entries with which to configure the command

  1. XCSourceEditorCommandClassName. The class that contains the extension command code
  2. XCSourceEditorCommandIdentifier. A unique identifier for the command. It is recommended to use a reverse DNS format.
  3. XCSourceEditorCommandName. The title that is displayed in the Xcode Editor menu that launches our command

In action

We will demonstrate one of the three commands the extension contains, specifically the one that generates a Swift struct from a JSON document.

Below these lines is the JSON document we used in the test, which can be found on the TheMovieDB developer website.

And in the following video, we can see how we launch the command and wait for ChatGPT to carry out the conversion. We warn you that it takes a few seconds 😀

Testing

To test our Xcode extension, we must select and run the extension schema. At that moment, a window will open in which we must select the application that will act as the host of our extension, which will be none other than Xcode itself.

Select Xcode as the app to run.

An instance of Xcode will open, but we will see that the app icon in the Dock is gray to differentiate it from the version running normally.

The commands of our extension are found in the Editor menu, and there we must go to be able to test the commands.

Xcode Source Code Editor extensions are available in the Editor menu.

For example, we will see the output of the Explain me this code command, where we ask ChatGPT to explain what a given code does.

Explain me this code extension result.

The command sends the file's content and adds the ChatGPT response as a comment at the end of the file, as you can see in the image above.

OpenAI API Key

The extension needs an OpenAI API Key to execute calls to ChatGPT.

This API Key must be stored in a file called openai.environment, which will only contain the key.

This file must be included in the target of the Xcode extension.

Source code

The code accompanying the article can be found in the following GitHub repository.

Links of interest

Thanks

To my colleague Pablo Calcagnino for this English translation of the original article.

Disclaimer

Please note that due to factors beyond our control, such as sudden increases in demand or unforeseen technical issues, the availability of the ChatGPT free API service may be subject to change without prior notice.

--

--

Adolfo
Globant

Me gusta leer, las camisetas y las zapatillas de deporte // iOS Software Designer @ Globant // Creador de MoveMAD, Ambiently, Shelves y alguna cosa más