Using Code Snippets in Swift

By Jesse Feiler

Code completion and Fix‐It can both help you speed up your typing and writing of Swift code. Code snippets in the library go even further: They are snippets of code you can just drag into your own code. You can use a snippet as‐is, but many have tokens — highlighted areas that you can customize with your own variable names or other customizations.

When you drag snippets with tokens into your code, they almost always generate errors because you haven’t replaced the tokens yet. This is normal.

In addition to the built‐in library, you can create your own snippets. Both methods are discussed here.

Working with built‐in code snippets

Here are the steps to use for working with built‐in code snippets.

  1. If necessary, show the utilities area.

    The utilities, navigator, and debug areas all can be shown or hidden independently, so what is visible depends on what you’ve been doing. The trio of buttons at the top right of the toolbar let you show or hide each of these areas.

    In order to make the library area as big as possible, you may want to drag the tab bar up, as shown.

    image0.jpg

  2. Click Snippets in the tab bar.

  3. Enter Swift in the search field at the bottom.

    The library contains snippets from several languages, so searching for Swift will make certain you’re working only with the Swift snippets.

  4. Select the snippet you want to use.

    Information about the snippet is shown.

  5. Drag the snippet into your source code and place it where you want, as shown here.

    image1.jpg

    If there are tokens in the snippet, you may get error messages until you enter your own data.

    In addition to replacing tokens, feel free to make any other changes you want to the code. When you’ve dropped a snippet into your code, it becomes part of your code. The fact that it came from a snippet rather than from your own typing doesn’t matter.

Creating your own code snippets

You can create your own code snippets and add them to the library. You can create these for any code you commonly use, from copyright notices to any other sections particularly relevant to your work.

Here’s how to create a snippet of your own:

  1. If necessary, show the utilities area.

    In order to make the library area as big as possible, you may want to drag the tab bar up.

  2. Click Snippets in the tab bar.

  3. Highlight the code you want to create as a snippet and drag it into the library, as shown here.

    image2.jpg

    Note that the figure shows the code of one of the functions in the template being dragged into the library: This is why you can see both the code in the file as well as the code in the process of being dragged.

  4. Select the snippet in the library.

    Information about the snippet is shown.

  5. Click the Edit button in the lower left, as shown.

    image3.jpg

  6. Add a good title for the snippet, and, unless the snippet you’re creating is language‐independent, like a copyright notice, change the language setting to Swift.

  7. Choose a scope from the Completion Scope pop‐up menu.

    image4.jpg

    Adding a scope makes your snippets even more useful because it prevents you (or your colleagues) from adding the snippet in illogical places.

    Set the language setting before choosing the completion scope. The completion scope choices in the pop‐up menu change depending on the language selected.

  8. (Optional) Replace the token text, such as the <#myVar#> shown.

    image5.jpg

    Token text can be more than one word. Until the token is replaced, your snippet will generate an error — again, this is normal.

    The following figure shows a custom snippet with a token (and the associated errors).

    image6.jpg