Contents

Writing code with intelligence in Xcode

Generate code, fix bugs fast, and learn as you go with intelligence built directly into Xcode.

Overview

The coding intelligence features in Xcode help you write code, navigate unfamiliar codebases, find opportunities for new features, fix or refactor existing code, and generate documentation along the way.

[Image]

You interact with a large language model using natural language prompts to ask questions and give instructions. The model refines the responses to your prompts based on your previous interactions and project context. You choose whether to use a third-party agent or chat product to help write code.

If you use an agent, Xcode can refine and iterate on a goal with less guidance and perform actions, such as fixing build errors after writing code. You decide what external tools, command-line or otherwise, an agent may use in responses to your prompts.

If you use a chat product, you stay in control over changes to your project by applying suggestions automatically or reviewing and applying them selectively yourself. Xcode maintains a history of your conversations with the model so that you can review past responses, track changes, and return to any previous state of your project.

Display the coding assistant and choose a third-party agent or chat provider

To open the coding assistant area in the sidebar, click the button to the right of the navigator button in the upper-left corner of the toolbar, or press Command-0. Here, you can enter prompts, see responses, start and navigate between conversations, undo changes, and more.

[Image]

If a Set Up button appears in the coding assistant, click the button and enable a coding intelligence agent or chat provider in Xcode > Settings > Intelligence, as described in Setting up coding intelligence.

In the coding assistant, click the Start New Conversation button on the left of the toolbar. In the pop-up menu, choose an agent under Agents or a model under Chat. The agent or chat model that you choose appears in the message text field below.

If you choose an agent, it automatically has access to some Xcode capabilities, such as building your app, searching Apple documentation, and more. When you enter a prompt, Xcode may display a dialog asking for permission to access files or use a tool before responding. For more information about settings for agents, see Restrict agent access to command-line and Xcode tools.

Explore unfamiliar code

At any time, you can ask Xcode to explain code and find files to implement a new feature or just to familiarize yourself with some code. For example, if you download the Landmarks: Building an app with Liquid Glass sample app, you can select code and ask questions, such as:

  • What does this app do?

[Image]

Xcode responds under your prompt in the conversation area of the coding assistant. The response may contain content that you can interact with. For example, if the response references a filename, click the arrow button next to the filename to open it in the source editor. To continue the conversation with the coding assistant, enter follow-up prompts, like:

  • Tell me more about the views that display this object

When you enter another prompt, the coding assistant appends your prompt and response to the conversation. Xcode maintains an entire transcript of your interactions with the agent or model so you can refer back to them.

Learn about symbols and code

In the source editor, Control-click a symbol or code selection and press Command-Option-0 or choose Show Coding Tools > Show Coding Tools from the contextual menu. Then click Explain, or enter a more specific prompt in the coding tools popover. The coding assistant displays the prompt and its response in the conversation area.

[Image]

Alternatively, click the coding assistant button in the source editor gutter to display the coding tools popover.

Enter a sequence of prompts to reach your goal

Give Xcode specific instructions on how to generate or modify your code. If you aren’t getting results that you expect, try breaking down your question or adding more detailed instructions.

For example, if you’re new to Swift and SwiftUI, you can code along with the modifications that Xcode makes. Start with a Swift app that you create from a template and instruct Xcode to make incremental changes, such as:

  • Add properties and methods to a class

  • Create a list view and wrap it in a NavigationStack

  • Add the ability to edit the properties of items in the list view

  • Change the list view to a table view showing all the properties

Between each prompt, review and validate the code changes, and continue iterating on your app by adjusting your prompts to get the results you want.

The response may contain next steps and ask you follow-up questions. You can either answer the questions (continue the conversation with the assistant) or enter a new prompt.

Generate or modify code

Enter your prompts in the message text field at the bottom of the coding assistant or press Command-Option-0 in the source editor and enter a prompt in the coding tools popover.

While working on a response, Xcode displays progress messages in the text field before posting its response in the conversation area. The response may contain a description of the changes, including some steps or code changes.

[Image]

If you choose an agent, Xcode may iterate on a response, build your app to verify the code, and fix build warnings and errors automatically.

The response may contain content that you can interact with. For example, click a code change to open it in the source editor. Xcode uses multicolor change bars to highlight changes made using intelligence.

To undo changes, click the Undo Changes button to the right of the message text field.

Apply changes to your code

If you use a chat product, you have finer control over when Xcode modifies your code.

The “Automatically apply code changes” button in the lower-right corner of the sidebar is on by default. If you turn the “Automatically apply code changes” button off, Xcode proposes changes to your code instead of applying them and labels them as “Proposal” in the conversation area.

The response may describe the code changes that the assistant suggests and contain proposed code that you can selectively apply or paste into your files.

To apply a proposed change, click the code snippet in the response and click Apply in the dialog that appears. If the change adds a new file, click Create New File in the dialog.

[Image]

Customize the context of your prompts

By default, Xcode automatically gathers relevant context to send to the model, based on your prompt and the conversation history. In addition to the automatic context, you can reference specific symbols and files, upload attachments, or refer to a selection in the source editor by mentioning it in your prompt.

You can add specific references to symbols and files by typing the @ character and choosing a symbol or file:

[Image]

To add additional files from outside your project, choose “Upload files” from the Attachments pop-up menu in the lower-left corner, under the message text field, and select the files to upload from the dialog.

If you use a chat product, a Project Context button appears in the lower-right corner of the sidebar and is on by default. This allows Xcode to share relevant code and other context from your project with the model. To narrow the scope of the project files, you can turn off the automatic search feature and add explicit references to files and symbols in your prompt instead.

Generate playgrounds and previews

Playgrounds and previews are a great way to experiment with new code without modifying your app. Use playgrounds to run and display code snippets in the canvas, and use previews to validate UI code across platforms. Xcode generates playground and preview code that may contain sample data to help you better understand and visualize the code in the canvas.

To add a playground macro to your project, open coding tools and choose Generate a Playground:

[Image]

Xcode shows the results of the playground, and for SwiftUI files, the previews, in the canvas area. If the canvas isn’t open, choose Editor > Canvas to show it, then click Resume.

[Image]

To learn more about the playground macro, see Running code snippets using the playground macro. For previews, see Previewing your app’s interface in Xcode.

Fix your code

If you encounter a compilation warning or error while building your app, Xcode may be able to generate a fix for you.

The source editor highlights any issues with a red underline and presents an issue summary and icon. Click the icon to show more information about the issue, then click Generate next to “Generate Fix for Issue”. Xcode applies the model-generated change and shows the fix in the coding assistant.

[Image]

If you choose an agent, Xcode may fix errors and warnings in generated code for you.

Generate documentation

Let Xcode draft your API documentation for you. In the source editor, select a symbol that needs documentation comments and click the coding intelligence icon that appears in the gutter. In the coding tools dialog, click Document.

[Image]

Xcode can add DocC-style comments to the source file above the symbol. For example, select a class and Xcode adds documentation for the class, its properties and methods, including the method parameters.

Xcode displays coding intelligence controls at the bottom of the source editor that summarizes the change. To see the response in the conversation area of the coding assistant, click the coding assistant button. To undo changes, click the Revert button.

[Image]

To view your documentation in Xcode, choose Product > Build Documentation.

Browse previous conversations

At any time you can review the conversations you have with the coding assistant. A conversation is a thread of prompts and responses that appears in the conversation area. For example, you can ask the model to make a series of changes for a feature you’re working on in the same conversation. Then create a new conversation for another feature that’s in a different part of your code.

You can manage your conversations in the conversation area or using the conversation pop-up menu in the middle of the toolbar to:

  • Review prompts and responses in the same conversation by scrolling up or down in the list of prompts.

  • Jump to a recent or previous conversation by choosing the conversation from the menu.

  • Remove previous conversations by choosing Clear Recents from the menu.

  • Start a new conversation by clicking the Start New Conversation button on the left of the toolbar and choosing an agent or chat product from the pop-up menu.

[Image]

Rollback changes using the conversation history

Use the conversation history that Xcode maintains to rollback changes to a known state of your project, or to review changes across multiple files in your project.

To rollback changes in a conversation by prompt, choose the conversation from the Conversation pop-up menu and click the History button. Xcode shows a chronological list of your prompts with a slider on the right. Move the slider from the bottom to the top to unwind changes in the order that you made them. Move the slider up to remove changes, that Xcode shows on the right, and move the slider down to restore changes in the next prompt.

[Image]

Xcode retains all the edits it made after that state in case you decide to roll forward any changes later. After scrubbing back to the point you would like to restore to, click the Restore button to update your project files to the state from this point on. To keep all the changes in a conversation regardless of the current slider position, click Cancel.

See Also

Coding intelligence