As developers, we can imagine ourselves as the head of an army, and the code is our army. Iron Man can have his own steel Army, and we can have a code army, if we know how to create our own template code 😊😊😊.

Template code can not only greatly improve our development efficiency, but also can unify the team code style, it is not easy to write bugs, especially the TableView, CollectionView that a lot of agents, we do not have to knock over and over again, of course, we can also customize some code templates in our own projects. Build Viewcontroller with one click.

Here’s how to build your own code army. Are you ready to be Iron Man? 😂 😂 😂

The following code, we should appear countless times in the project, do we have to lose every time? In particular, the cell initialization method, dequeueReusableCell, is not always remembered, sometimes forget to go to other places to copy and change (I also do this kind of mess 😂😂😂), but if we define this section of code as a code block, can be typed with one click.

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String.init(describing: CustomCell.self), for: indexPath) as! CustomCell
        return cell
    }
}
Copy the code

Select the Code that you want to save as a Code block, right click, Create Code Snippet,

That Completion is the name of the code block that you set, and it’s kind of like a native code association, and you can do that by typing in a tableviewDelegate, and you get the code association, and then you press enter and you enter the complete code block.

Here’s another problem. Our code block is fixed. For example, we have CustomCell in our sample code, which should be mutable and fillable like the native interface. Can we do that? Yes. Very simple, go back to where you need to set the code block in the beginning, wrap it with <##> where you need to set the mutable code, such as <#CustomCell#>, there is a little trick when typing, the # on the right is written last, wait until the CustomCell is finished, otherwise you will wrap a piece of air 😅😅😅, (Don’t ask me why I mention this, because I stepped on thunder and couldn’t write it out a few times at first), you’ll notice that the block is highlighted and can be typed.

Now we can type in the perfect Tableview proxy with one click, as simple as writing a variable. Enter the table association has come out, directly enter the above code is finished.

The first step to build a code army is complete!! I’m going to leave you with the imagination space, put in all the code that you don’t want to write, and basically put in all the possible carbon copies that make sense. The second step of building a code army begins.

Sometimes it’s not convenient to have a code block, maybe I want to type the entire Viewcontroller in one click, maybe the team structure is stable, maybe there’s some common code in the Viewcontroller, but I can’t pull it out, and I don’t want to write that code, for example, these methods are pretty common, Almost every time I create a ViewController, I have to write all of these methods, so I don’t want to write them, but can I create a ViewController that has all of these methods in it with one click? Create a ViewController like you normally do, but automatically give me these methods, right? Come on, keep up.

Go to folder

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate
Copy the code

So let’s just click on one of these, UIViewControllerSwift, and let’s open it up.

Is there a sense of deja vu? 😀😀😀 is a native UIViewController template? So you might be thinking, WELL, I can do whatever I want here and make my own Viewcontroller right now, but what if you want to go back to the original Viewcontroller? The system comes with the file better not to change, we create a new and then change, that we create where… 🙄 🙄 🙄

Did you notice the top line when we created a new file with CMD +n? Choose a template for your new file, create a template for our new file, and look at the following: title, Source, User Interface, Apple Watch, Resource, Aren’t these titles exactly like File Templates in the directory we just went to? Does that mean we can add a custom one?

PNG, templateIcon.png, templateInfo.plist. These seem to be related to each other, so let’s try them out. Customize the “Custom Templates” folder and put it in the Templates directory. Then copy the entire Cocoa Touch Class.xctemplate folder.

After that, restart XCode, CMD +n, we can see that there is indeed a custom template, then we can modify the contents of the file, open the file we wanted to edit, but found that it cannot be edited. This is because the folder in which it is located makes it uneditable. We can edit it by copying it to the desktop and then replacing the original file.

Finally, create a custom ViewController with the following effect:

😋😋😋 really fragrant, so we can customize the template, the vast sky is free to you. Xctemplate folder must have images and plist files. The suffix of the image file seems to be ok, as long as it is an image file, but the file name TemplateIcon is not changed, and the plist file basically does not need to be changed. I put the file under Application later, and found that it looks better. And it’s right at the top, looking at personal habits.

Finally, these files will disappear when XCode is updated, so we can write a script that will run the script after the update.

Script code is as follows, open the terminal, CD to the directory, directly drag the script into the terminal, enter the computer password on the line.

#! /bin/bash
sudo cp -R Custom\ VC.xctemplate /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project\ Templates/iOS/Application/

sudo cp -R Custom\ View.xctemplate /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project\ Templates/iOS/Application/

Copy the code

After learning the above, build your own code army is just around the corner!! Click “like” and then go 😀😀😀