Make App Pie

Training for Developers and Artists

Use Code Snippets

An often neglected feature of Xcode is code snippets. Code snippets are quick ways to add code you frequently use. In Xcode 10 there were some major changes to code snippets, which might confuse those who use them now, but make them even easier to use. Let’s take a look at this cool feature. 

I’m using the Beta version of Xcode 10 here. As I’m using beatas, be aware changes from what I’m telling you might happen, but I’m not expecting any at this point. Download the starter file, which is a table view controller. I’m not a big fan of Xcode’s table view controller template, so I usually take a blank view controller and type all this in every time. Snippets can save this code in Xcode and every time I need TableViewController code, I just pop it in.  I also have my import UIKit and my signature I use often. Lets save each of these as snippets, then use them in a new view controller. 

I’ll save the signature first as a snippet. In Xcode, highlight the   comment code. 

To save this as a snippet, I’ll  highlight what I want in the snippet. In this case, I’ll make this the entire comment, right-click and create code snippet. 

 

2018-09-05_06-41-10.jpeg

 

The snippet library opens up with a dialog box for naming the snippet. I’ll set the title to Tips Signature and then summarize the signature so I remember what it is. 

Comment signature for weekly tips. 

I’ll also add a completion shortcut of tipssignature. Completion shortcuts will insert the snippet anywhere I type this text. I left this all small to make it simple to type, and to prevent it from looking like a keyword. Below that is an editing window showing the code I’m adding. 

2018-09-05_06-42-06.jpeg

I’ll press return to save the snippet. 

I’ll do the same thing for import UIKit.  I’ll make the title and summary import UIKit  and I’ll make the Completion Shortcut uikit. Now this only applies to iOS and Swift, So I can change the platform to iOS  and change the language to Swift to restrict when it is available. 

2018-09-05_06-44-55

 I’ll highlight the class  and create a code snippet. I’ll give this a title UITableViewController and then a Summary to describe this as a basic template for a table view controller.  I’ll set the platform to iOS and language to Swift. The completion template will be tableviewcontroller. I’ll also set the completion scope to Top level. This sets when the completion scope works. I don’t want this as a class inside of a another view controller. 

 

My table is still pretty specific. I’d like to prompt myself to change the class name.  You can use a mark to do that. I’ll start with the class name and use a <# to start the mark, add TableName to describe it and end with #> , so I’ll add <#TableName#>. The text immediately changes to a place marker.

2018-09-05_07-06-51.jpeg

I can do the same through the document, to data. 

Close the library, and open a blank swift view controller. Delete the code the with Command-A delete.  I’ll use three snippets I set up. There three ways to use them. The first I’ll do with he signature. You can click the library icon {} at top, and drag it in.

 

2018-09-05_07-10-11

 

Secondly you can just type it in. I’ll type uikit and return. Thirdly the completion phrase shows on the autocomplete. I type tablevi  and then select it from the autocomplete. 

2018-09-05_07-12-33.jpeg

I have my new view controller, But it has place markers. 

 

2018-09-05_07-14-11.jpeg

I’ll fill in the name of the class and SecondTableViewController and the identifier for the data as data. 

Snippets stay in Xcode, not in your project, so you can use them for many projects. Snippets can be used for bigger code pieces like this or smaller ones. There’s many Apple made for you for convenience that you might want to scroll through as we’ll.  This will speed up your coding in ways you may not imagine. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: