
If we want to remove a custom code snippet from the library, we can select with left-click the one which we want to remove and then press the button delete ⌫. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // MyObject.swift // // Copyright © 2017 Marco Santarossa () // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. We may use this option for the implementation of a delegate-like UITableViewDataSource: It would work also in structs and Swift extensions. We can decide if the shortcut should work just inside a class implementation. We may want some specific scopes, and we can also combine them with + button: By default, the value is All, therefore it works everywhere. We can decide if the shortcut should work just in a specific scope. By default, the language is the one used in the project. We can decide in which language the shortcut should work. We may have two code snippets with the same shortcut, one for iOS and one for macOS. We can decide if the shortcut should work just with a specific platform like iOS, macOS and so on. In this way, if in our code we type t1 then the autocompletion will suggest our snippet: We’ll have the following result in the library:īefore explaining the other options, we must understand how to assign a shortcut, because the other options work just when we add a snippet using a shortcut-the drag & drop ignores the options.īy default, this value is empty, therefore we can’t use a shortcut to add the snippet. Thus, the library becomes confusing and we’ll have difficulties to find a specific snippet. The problem of leaving the default title is that if we add more than one snippet, we will have a lot of My Code Snippet. By default the snippet has the title My Code Snippet.

These two fields allow us to add a title and a subtitle to our code snippet. In the edit mode we can also change several options for each custom snippet. If we want to add a placeholder token in our code snippet we can add in the body of our snippet-change token_text with whatever you want: You can easily change the placeholder focus with the Tab button to move to the next one, whereas with Shift+ Tab you can move to the previous one. If you click one of them and start typing something, you’ll notice that you can easily replace the placeholder with what you’re typing. Name, parameters, return type and function body are called placeholder tokens. If we type func inside a class and press the button return ↩ in first suggestion of the autocompletion, we will have the following result: Once we add a new snippet, we’ll be able to edit it with a double-click and clicking the button Edit: With shortcuts-we’ll see in the section “Options” how to assign a shortcut. Select it with left-click in the Code Snippet Library and drag inside our code: There are two ways to use a code snippet:

You can watch the demonstration in the following video: How To Use A Snippet If those are not enough for our needs, we can add custom ones. We can find this library in the Utilities Menu:īy default, Xcode provides several snippets. The Code Snippet Library is a list of code snippets available in Xcode.

Happy Reading! 👍 Code Snippet Library What Is It? In this article, I’ll describe how Xcode manages the code snippets and then how to create a custom one using also advanced options provided by the IDE. In this way, we can reuse these snippets easily thanks to shortcuts or drag & drop. The concept of code snippets is saving a piece of code which we write often.

Do you write hundreds of lines of code every day? Would you like saving time typing less code? Well, Code Snippet Library may become your favourite Xcode feature.
