keepcalculator

06 Numbers and Powers

Xcode Code Completion: How It Works & Fixes

Card No. 06·72654 · Updated · Standard formula, shown below

Xcode’s code completion (also called autocomplete) suggests symbols, keywords, and even entire code snippets as you type in the Swift or Objective-C editor. You trigger it by simply typing, and you accept a suggestion with the Tab or Return key. This article explains how it works, how to customize it, and what to do when it stops responding.

What Code Completion Does in Xcode

Code completion is a feature of the Source Editor in Xcode that predicts what you are likely to type next. It reads the current context—the file you are editing, the language (Swift, Objective-C, C, C++), and the position of the cursor—and offers a list of valid completions. These can be:

  • Local and global variable names
  • Function and method names with their parameters
  • Type names (classes, structs, enums, protocols)
  • Keywords like if, guard, return
  • Code snippets (e.g., a full for loop or a SwiftUI view template)

When you press Tab or Return (depending on your settings), Xcode inserts the highlighted completion. If there are multiple matches, you can use the arrow keys to move up and down the list. You can also dismiss the popover by pressing Escape.

How Xcode Generates Suggestions

Xcode builds its completion list from a mix of static analysis and indexing. The compiler’s AST (Abstract Syntax Tree) provides the exact set of valid symbols in the current scope. The indexer scans your project and all imported frameworks to know the names of types, functions, and properties. For Swift, the compiler also provides “completion tokens” that let Xcode show contextual results—for example, after typing someView., it lists only the members of that view.

For expressions like UIView, Xcode uses the type checker to infer the type and offer only valid members. This is why completion sometimes feels slow or incomplete: the type checker must run in the background. In large projects, this can take a few seconds. For SwiftUI, the preview compiler and the main build system both contribute to the index, so completion may be more accurate after a successful build.

How to Use Code Completion Effectively

There are a few keyboard shortcuts and behaviors that make completion faster to use:

  • Tab vs. Return: By default, Tab accepts the highlighted completion and continues with the next placeholder (e.g., a parameter). Return also accepts but may insert a newline if you are not in a completion session.
  • Escape to dismiss: If the popover is in the way, press Escape to close it. You can bring it back with Control+Space.
  • Control+Space: This manually invokes completion at the cursor position, even if Xcode hasn’t popped it up automatically.
  • Option+Escape: In some Xcode versions, this shows a more detailed list of completions with documentation.
  • Filter as you type: The list narrows as you type. For example, typing numberOfRows will match tableView(_:numberOfRowsInSection:) even if you only type numRows because Xcode uses fuzzy matching.

You can also accept a completion partially: if you want only the symbol name without the parameter placeholders, press Escape after inserting, or use the “Insert without placeholder” option in the Edit menu (if available).

Customizing Code Completion Settings

Xcode lets you adjust how completion behaves in the Text Editing preferences. Go to Xcode → Settings → Text Editing → Editing (or Preferences in older versions). There you will find:

  • “Enable type-over completion” – This is the master switch for code completion. If you turn it off, Xcode stops suggesting automatically.
  • “Automatically insert closing braces” – This adds the closing } when you type an opening one, which affects how completion interacts with braces.
  • “Use Esc to show completion” – Some versions have a separate option for the Escape key.

You can also control the font size and the number of lines in the completion popover from the General tab, but the most useful setting is the delay before the popover appears. In older Xcode versions, there was a slider for “Completion delay” in the Editing tab. If you find the popover too intrusive, increase the delay; if you want it faster, set it to a lower value.

For per-project settings, you can set the “Indexing” and “Build” options in the project editor. If you use a custom build configuration, make sure the “Index” setting is enabled for that configuration, otherwise completion will be incomplete.

Common Problems and How to Fix Them

Code completion can stop working or become inaccurate. Here are the most frequent issues and their solutions:

  • Completion is empty or shows only keywords: This usually means the index is out of date. Try Product → Build (⌘B) to rebuild the index. If that doesn’t help, go to File → Workspace Settings (or Project Settings) and click the “Delete” button next to “Derived Data”. Then reopen the project.
  • Completion is slow: Large projects with many files can slow down the type checker. Try enabling “Indexing with Precompiled Modules” in Build Settings. Also, make sure you are using the latest Xcode version, as performance improves over time.
  • Completion shows wrong symbols: This can happen if the file has a syntax error. Fix the error and completion will often correct itself. Also, ensure the target that contains the file is selected in the scheme (top-left dropdown).
  • Completion doesn’t appear for SwiftUI views: Sometimes the preview compiler needs to run first. Click the Resume button in the preview canvas, or just build the project.
  • Completion works in one file but not another: Check that the file is included in the target. If it’s not, the index won’t know about its contents. Look at the File Inspector (right panel) and make sure the target membership checkbox is ticked.

If none of these work, try restarting Xcode, or deleting the Derived Data folder entirely (be aware that this forces a full rebuild).

Code Completion vs. Snippets and Quick Help

Code completion is often confused with two other features: code snippets and Quick Help.

Code snippets are pre-written pieces of code that you can insert by typing a shortcut. For example, typing for and pressing Return might insert a full for loop. You can manage your own snippets in the Library (the plus icon in the toolbar). These are different from completion because they insert an entire block, not just a symbol.

Quick Help (Option+Click on a symbol) shows a popover with the declaration, documentation, and parameters. It doesn’t insert anything. Both features rely on the same index as completion, so if completion is broken, Quick Help may also be affected.

Alternatives and Third-Party Tools

While Xcode’s built-in completion is powerful, some developers prefer alternatives:

  • SourceKit-LSP: This is the language server that powers completion in editors like VS Code. You can use it with Xcode’s own toolchain, but it’s not a full replacement for Xcode’s editor.
  • CodeSense and other plugins: There are third-party plugins that promise better completion, but Xcode’s plugin ecosystem is limited and often breaks with new versions. As of recent Xcode versions, many plugin APIs are deprecated.
  • Using a different editor: Some developers write code in AppCode (a JetBrains IDE) or VS Code with the Swift extension, then build in Xcode. These editors have their own completion engines, which can be faster or more accurate in certain situations.

However, for most iOS/macOS development, staying with Xcode’s built-in completion is the safest and most integrated choice, especially because it understands SwiftUI and the full SDK.

Frequently Asked Questions

Why is my code completion not showing up at all?
Check that “Enable type-over completion” is on in Text Editing preferences. Also, make sure the file is saved and the project has been built at least once. If it still doesn’t appear, delete Derived Data and rebuild.

How do I make code completion show more suggestions?
Sometimes the list is limited because Xcode is unsure about the type. Try adding an explicit type annotation, e.g., let view: UIView = ..., which gives the type checker more information. Also, make sure the project indexes successfully—look for errors in the Issue navigator.

Can I change the shortcut for accepting a completion?
Yes. In Xcode → Settings → Key Bindings, search for “Complete” and you can reassign the “Accept” action. The default is Tab, but you can change it to Return or any other key.

Why does Xcode sometimes insert a semicolon or a closing brace automatically?
That’s part of the “Automatically insert closing braces” and “Enable type-over completion” settings. You can turn off the brace insertion in Text Editing preferences if you prefer to type them manually.

Does code completion work for Objective-C as well?
Yes, Xcode supports completion for Objective-C, C, and C++. The behavior is similar, but the type inference is less powerful than in Swift, so you may see more generic suggestions.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →