Snippets are reusable blocks of code and can be used across projects to avoid typing the same code more than once. This video shows how to get snippets working in your projects.
- Let's say you work in many different projects, and you use some kind of utility method often in your projects. And you find yourself having to type this out every time you create a new project, or maybe copy and paste that from another project. Xcode actually has a way for you to save these code snippets to a library for easy use later on. To do that, you're going to need to show the code snippet library which you can show by showing the utility area, and then looking down in the library section which is at the bottom of the utility area, and going to the code snippet library, which is the open and closed curly braces.
So once you're in there, and you have some code you want to save, select and highlight the code. Hold your mouse down on it for a second. Click and drag that into the library, and release your mouse button. So in here the first thing to do is to give it a title. I'm just going to call this Example Method. Give it a summary, or description, you can narrow it down to specific platforms if you want, so it's just for one platform, like iOS, or MacOS, you might choose that, but if you want to apply to every platform you can select all.
If you want to say it's a specific language, you can do that by clicking this drop down. So maybe you want to specify that it's Swift code, so you can choose that, or you can just leave it at generic. Now, to custom complete it in your code, you need to write out a completion shortcut. So we'll just call this Example. Then we set completion scope. So hit that drop down, and if you don't see all of these options, you can just hit done and then double click this to open up the editor menu and change it.
Sometimes you might just see a couple of them. One time I opened it up and I saw just String or Comment and Top Level. So you may have to open it again to see all of the options. So we can narrow the scope down. So maybe we don't want to be able to write this code in every single scope, we just want to be able to write it when we're working in a class implementation. So you can choose that in that case. Or maybe you have a couple of scopes you want to use, but not all of them. In those cases, you can hit the plus button, add an extra scope, and save it out.
If you don't want to add that, you can always hit that minus button and remove it. So we have my completion scope set, a completion shortcut set, my code set, which I can edit later on if I ever want to, and then I hit done. So now I have my example method. If I want to put it in my code, I can just click and drag that out, and there it is. I can also delete this code here, and start typing my completion shortcut which is Example. And when I type that, it says I'm typing out Example, which is my completion shortcut, and that is the completion shortcut for Example Method.
So if I press return on that, boom, there it is in my code. If I ever want to change this later on, I can simply double click that in the code snippet library, and then hit the edit button. I can make any changes to the code I want in here, I can change the title, completion shortcut, et cetera, and hit done, save the change. If you want to get rid of a shortcut that you've made, simply highlight it, you can see that it's blue when it's highlighted, and hit the delete key on your keyboard, and just confirm the deletion.
First, learn how to install Xcode and create a new project. Then find out how to edit code; leverage helpful features like the Assistant Editor, playgrounds, and snippets; and integrate version control with GitHub. Discover how to use Interface Builder, Xcode's intuitive UI design tool, to design a responsive interface that adapts to screen size, aspect ratio, and orientation changes. Todd also shows how to map application flow with storyboards, and reviews the basics of schemes and behaviors. Plus, learn how to compile and debug your code, test apps in the iOS Simulator, and submit apps for publication in the App Store directly from Xcode 9.
- What is Xcode?
- Installing Xcode 9
- Creating your first Xcode project
- Editing code
- Creating snippets
- Making interface connections to code
- Using Interface Builder
- Creating storyboards
- Pinning objects
- Compiling code with the LLVM compiler
- Working with schemes and behaviors
- Debugging code
- Sharing resources in a workspace
- Using the iOS Simulator
- Preparing an app to be published