Learn how to make code snippets to speed up coding.
- [Instructor] An often neglected feature of Xcode is code snippets. Code snippets are quick ways to add code you frequently use. In Xcode 10, there's some major changes to code snippets, which might confuse those who use them now. But we'll make them easier to use. Let's take a look at this cool feature for everyone. I'm using a beta four version of Xcode 10 here. And as I'm using betas, be aware changes from what I'm telling you might happen. Download the starter file, which is a table view controller. You can click on ViewController.swift.
And get rid of all the other stuff 'cause we're gonna stay right in the view controller here, so you can see everything. I'm not a big fan of the table view controller template. I usually take a blank view controller and type all this in every time, which is the numberOfRowsInSection, cellForRowAt, and indexOath, didSelectRow, and numberOfSections. Snippets can save this code in Xcode every time I need the table view controller code and I can just pop it in. I also have my import UIKit and my signature, which is different than Apple's that I use often.
I'm gonna save each of these as a snippet and then use them in a new view controller. I'll save the signature first as a snippet. In Xcode, what I'll do is I'll just highlight this code, right-click, and hit Create Code Snippet. The snippet window opens and you'll see that it's got a second window over here for me to define this a little better. It always defaults to My Code Snippet for the title, but I'm gonna change the title here to Tips Signature.
And then it's gonna ask me for a summary after that and I'm gonna say my Common Signature For Weekly Tips. So I can put that in like that. I can also add something called the completion shortcut. And I'm gonna make that tipsignature, which will let me, when I do an auto-complete function in my code, type that in and it'll just show up automatically.
Notice I did not camel case this, so it does not look like a keyword. And then below that is an editing window, which shows me exactly what I'm gonna be putting into the snippet. And it's actually editable if I need to. I'm gonna go ahead and press Return to save this snippet. And you'll see over here, I've got a new snippet. I'm gonna go click back on my text here. I'm gonna go ahead and highlight my import UIKit. I'm gonna do the same thing I just did.
Create Code Snippet. And then I'm gonna change this one to import UIKit. The summary's pretty simple here. I'm gonna do the same thing with the completion shortcut is I'm just gonna do uikit all in lower case, so I can use that as my completion shortcut. Now, this is only gonna apply to iOS and Swift, so I can change some other two things. The platform tells me which platform this will be applied to and this will only apply to iOS.
So I can change that here. And I can also change it to a language. As you can see, we've got lots of different languages to play with. I'm just gonna go down here to Swift and put it there. So this will only work in Swift. So I can change my ranges of how to do this. And then once I've done that, I'm good to go. And I'm done with this one. And now, I've got my import UIKit one done. I'm gonna click outside here one more time and now, I'm gonna highlight the table view controller. And I'm gonna highlight the whole class, so I can pop a whole class in here.
Once again, I'm gonna create the code snippet. And this one's going to be a title of UITableViewController. The summary is going to be Basic Template For a Table View Controller. Again, I'm gonna use iOS and Swift for my language.
I'll make a completion of tableviewcontroller like so. And then I'm gonna do one more thing. This table is pretty specific. I've got data already here. I'd like to prompt someone, instead of saying just TableViewController, I'd like to edit down here to put a name for a table view controller. So I wanna say the second view controller or the ice-cream view controller. Now, I can do just like you do in Xcode, have those little place marker completion things. And you could put one into your snippets as well.
And you gotta do a left angle bracket, hash, and then type a word in there. And I'm gonna do TableName. Then do another hash mark and then do a right angle bracket or Shift-period. And pop like that, you've got yourself a place marker. I'm gonna do that again over here for data. And I'm just gonna do it right over this data point here. So I'm gonna do it just like this.
And I got a couple of these. Hash mark, Shift-period or right angle bracket. I should have one down in here. Right there. And I think that's all I did. I didn't do any of the select yet. Okay, so that's all done. And I can go ahead and just click over here and close that up. And you see I've got now a basic template, which I spelled wrong, so I'm gonna click on this again and edit in here like so.
And now, I've got it all set. And you'll see I've got the three user ones on top here. And you can search your snippets any way you want up on top here. All right, I'm gonna click outside here. I'm gonna go to File, New, File. I can use either a Swift file or a Cocoa Touch Class for this. I'm gonna just a Swift file for this. I'm gonna go hit Next. I'm gonna change this to SecondViewController like so and then I'm gonna save it in my group CodeSnippets.
That's great, go ahead and create. And that gets me a second view controller. I'm gonna take all that code and just delete it. All right, and now, I've got a blank page. I'm gonna show you what we can do with a blank page. I'm gonna bring up the snippets. And the first thing I can do, is for the tips signature, I can select the tips signature, drag it up here, let it go, and there's my tips. I'm gonna go down here to line nine, 10. Make an extra space, do the same thing.
And I can double-click on the import UIKit as another way of doing this. But the interesting one to follow is this one. Is I can do uikit, Return, and bang, just like that, I've got my import UIKit. I'll make two more spaces. And now, I'll start typing tableviewcontroller and you'll notice, up in the auto-completion, you've got a UITableViewController right here. It doesn't have anything next to it for the class, so that's the one you want. I can just click that and then I get my table view controller.
You'll notice it gives you lots of errors because of all the place markers. I can click on a place marker here, change it to Second for my class. I'm gonna call this data, which I can actually do very quickly by just double-clicking on it. And I have a view controller. That was all it took. Snippets stay in Xcode, not in your project. So I can go ahead and close all this up, use a different project, and those snippets will be back in every project I have.
So snippets can be used for bigger code pieces like this or smaller ones. There's many that Apple actually made for you, as you can go through all this list here, for convenience that you might want to scroll through as well. Snippets will definitely speed up your codings in ways that you may not imagine.