Completing Code with Code Completion in Swift

By Jesse Feiler

Xcode “watches” your keystrokes, which enables it to suggest code as you type. This feature is called code completion, and it’s a great time‐saver. Here are some examples of its use, along with some things to watch out for — just as you can sometimes make mistakes, so can Xcode’s code completion!

Code completion is the name of the technology. In the user interface of Xcode, however, it’s referred to as Fix‐It.

To create a new project, first launch Xcode and choose File→New→Project, select a template, and choose a name and location for your new project.

Similarly, you can create a new playground by launching Xcode, choosing File→New→Playground, selecting Swift as your language, and entering a name and location for the playground.

Using basic code completion

The following steps take you through a demonstration of code completion. This demonstration uses a playground:

  1. Create a playground, as shown here.

    image0.jpg

    You can base it on iOS or OS X.

    You can delete the initial text if you want. Playgrounds begin with a comment, an import statement, and a string variable set to “Hello, ­playground.” Leave those or not, as you wish.

  2. Begin typing in the code to create a variable — var myVar: String. Slowly type var myVar: S.

    Go slowly to allow code completion to do its work (not because code completion needs a long time to work, but because going slowly will give you a sense of how long the process takes on your Mac with your specific configuration). Shortly after you type the S, you’ll see that the rest of the word String is filled in using gray text. Depending on your preferences, the S may be blue (the default value) or some other color, whereas the suggested completion (String) is gray.

  3. From here, you can do any of three things:

    • Accept the completion: If the suggested completion (String) is what you want, press Return and the suggestion is accepted. The gray of String changes to the same color as the initial S.

    • Hesitate: On the other hand, if you hesitate before accepting the suggestion, additional completion suggestions will appear in a list on top of the playground, as shown here. Note that the list of additional completions is scrollable: A lot more choices are available than the ones shown.

      image1.jpg

    • Continue typing: If you continue to type Strin, the list of completion suggestions gets shorter, as shown. Click a suggestion from the list, and it will replace the text in the playground. At any time, you can press Return to accept the suggested completion.

      image2.jpg

Using code completion in a project

Code completion doesn’t just look at what you’re typing; it also looks at the context of what you’re typing. The following steps show you how code completion interprets according to context:

  1. Begin by creating a new project based on the iOS Master‐Detail Application template.

  2. Open the project navigator at the left of the workspace window (if it’s not already open) either by using View→Show Project Navigator, –1, or the button at the right of the toolbar to show or hide the project navigator.

  3. If necessary, open the project folder in the project navigator.

  4. If necessary, open the project group (it has a folder icon, but it’s a group).

  5. Scroll to the top of the AppDelegate.swift file, as shown here.

    image3.jpg

  6. Just below the var window: UIWindow? line, begin typing in the code to create a variable — var myVar: String.

    A list of code completion suggestions appears, as shown. New choices — UnsafeMutablePointer<StringPtr> and UnsafeMutablePointer<UInt8> — now appear in the list. These completions in the Xcode environment are possible because of the syntax that surrounds them.

    image4.jpg

Working with code completion

Code completion changes and evolves over time. Even if you’ve used it before, you may find it has features that weren’t available when you first learned it. To get the most out of it, try some of these tips:

  • Pay attention to the color of the text as you type: Settle on a style you like and stick with it so that you can recognize when the coloring changes. If the coloring looks wrong, Xcode is probably not recognizing your syntax — the most common reason for this is that you’ve entered a typo. For example, if you type myVar: S instead of var myVar:S, the S will remain uncolored (black by default).

  • To regenerate the code completion suggestions, use Delete to backspace to a point in your code before the first letter that seems colored correctly: For example, if you want to regenerate the suggestions for S, back up to the space before it and retype S.

  • Consider using code completion even if you’re a fast typist: If you type quickly, you may not think you need code completion — but don’t be so sure. Not only does it save you keystrokes, it also completes the code accurately, and fast typists can always make mistakes.

  • Remember that code completion is accurate — but it’s not infallible: Code completion doesn’t make mistakes, and yet sometimes its completions are wrong.

  • Always look at the suggested completion before pressing Return: Most of the time it’s either correct or it’s an obvious misinterpretation of your intentions. But sometimes, you do have to intervene to correct a misinterpretation, and in those cases, you must type the code yourself.