If you regularly watch WWDC videos, you might spot a detail. It’s these presenters who share code content and are able to quickly type in large chunks of pre-programmed code, which is pretty cool to watch. Xcode itself provides this functionality, allowing you to customize snippets of code and reuse them as needed. This feature is not only useful for delivering speeches, but also greatly improves efficiency and reduces unnecessary rethinking in our daily development.
Xcode itself has a lot of code snippets built into it, as shown below. Under the right bar, click on the {} TAB:
It lists some of the code snippets built into Xcode that we’ve already used in our daily development. Like this fragment template:
It’s also easy to use these preconfigured snippets, which can be dragged directly from the right bar:
Of course, in addition to dragging, you can also type code keywords:
The example of reusing built-in snippets mentioned above is a relatively simple scenario, but it also helps us improve our efficiency. The next thing I want to talk about is how to use snippets of code to reduce some of the repetitive thinking. For example, UICollectionView is a component that you’ll probably use a lot, but it requires a lot of code to create. Here’s a simple example:
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 2
layout.minimumInteritemSpacing = 2
layout.itemSize = CGSize(width: 200, height: 200)
collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CellIdentifier")
self.view.addSubview(self.collectionView)
Copy the code
As you can see, even the simplest UICollectionView create a, also need to undertake a series of initialization, such as creating UICollectionViewFlowLayout is used to specify its layout, the layout is a must have. You also need to register the Cell class for it, as well as the ReuseIdentifier.
Any loss of this initialization code can cause problems at run time, making debugging difficult. However, it is difficult for us to remember such tedious code all the time. I used to refer to the old code or examples on the Internet when I used it.
Which brings us back to how to reduce rethinking. Now that we know about Xcode’s snippet capabilities, we can actually create a snippet for it and use it when we need it in the future, so we don’t have to look around and remember.
Creating code snippets is also easy. Select the code we want and drag it backwards to the right:
After dragging to create, a small window will pop up, which lists some property fields. Take a look:
The Title and Summary properties are the Title and Summary of the snippet and are displayed in the right sidebar and in the code prompt screen. We can also select the Platform and Language of the fragment, and we can specify the Platform and Language to use the fragment.
Completion Shortcut this is the Shortcut for the snippet, and we specify coll here, so in the code editor, after we type the same keyword, we can select the snippet in the prompt menu, and the snippet is the first item in the prompt menu:
Now that we’re more than halfway there, you can use the snippet by typing in keywords or dragging it from the right bar. However, there is still some flexibility, such as itemSize, minimumLineSpacing, etc., the value of these attributes are written dead. We may not want to use these preset values, but you can change them manually, and Xcode also provides a more flexible solution.
You can use the <# #> symbol in snippet templates to identify a placeholder with an identifier, such as the variables in our template.
After this modification, when we use the code snippet again, you should see that all the parameters are highlighted and you can assign values to them in turn:
This highlight should be familiar to you.
I talked to you about Xcode’s snippet capabilities and how they can be used in our daily development. It allows you to write code that is more standardized and less error-prone, especially when it comes to tedious snippets that are often used. Reasonable use, will certainly make your development experience more and more cool.