Xcode custom template

For a long time did not write articles, because there are some trivial life, but also because the work is busy. Today we talk about the Xcode custom template.

Why custom Xcode templates

Sometimes the company will require a certain code specification, such as the controller needs to use #pragma mark – to split the various methods, like this:

#pragma mark - LifeCycle - dealloc (void) {} {} - (void) viewDidLoad - (void) viewWillAppear: (BOOL) animated #pragma mark {} - UITextFieldDelegate - (BOOL) textFieldShouldReturn: (UITextField *) textField #pragma mark Public #pragma mark {} - Private - #pragma - Getter - mark (NSString * name #pragma mark - Setter) {} - (void) setName: (NSString * name) {}

This can not only split the code, easy to read, and can easily find a method:

Xcode custom template

But every time you create a class to write this again is not a bit cumbersome, this time you want to use Xcode custom template function.

Modify system template style

The path Xcode system template is /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/, inside the folder file template File Templates and Project Templates project template, corresponding to creating file options and create project options:

Xcode custom template
create file interface
Xcode custom template
create engineering interface

We use the most is File Templates/Source/Cocoa Touch Class.xctemplate inside the template. It’s like this:

Xcode custom template
Cocoa Touch Class.xctemplate

For example, today we need to change the
UIViewController template, you need to change the UIViewControllerObjective-C, UIViewControllerSwift, UIViewControllerXIBObjective-C, UIViewControllerXIBSwift. Their differences can be seen from the name, that is, OC and Swift, the difference between the use of Xib.

We do not use OC and Xib, for example, open the ___FILEBASENAME___.m file in UIViewControllerObjective-C:

/ / / / ___FILENAME___ / / ___PROJECTNAME___ / / Created / / by ___FULLUSERNAME___ on ___DATE___. //___COPYRIGHT___ / / #import @interface ___FILEBASENAMEASIDENTIFIER___ ("___FILEBASENAME___.h") @end @implementation ___FILEBASENAMEASIDENTIFIER___ (void) viewDidLoad {[super viewDidLoad]; any additional setup after loading / Do the view. (void) {didReceiveMemoryWarning} - [super didReceiveMemoryWarning] Dispose of any resources that; / / can be recreated.} / * #pragma mark - Navigation a Storyboard-based / / In application, you will often want to do a little preparation before navigation (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (ID) sender Get the new view controller {/ / using [segue destinationVi EwController]. Pass selected object to the / the new / @end view controller.}

Is not very familiar with, that is, we create a controller after the code inside the.M. Some of these macros are:

The macro name Significance
___FILENAME___ File name includes suffix
___PROJECTNAME___ project name
___FULLUSERNAME___ User name
___DATE___ Current date, year / month / day
___COPYRIGHT___ copyright
___FILEBASENAME___ File name without suffix
___FILEBASENAMEASIDENTIFIER___ File name without suffix

This file is not allowed to modify the default, we can modify his permissions, or copy a copy to the other place, modify the replacement of the original file. Modify the contents of the:

/ / / / ___FILENAME___ / / ___PROJECTNAME___ / / Created / / by ___FULLUSERNAME___ on ___DATE___. //___COPYRIGHT___ / / #import @interface ___FILEBASENAMEASIDENTIFIER___ ("___FILEBASENAME___.h") @end @implementation ___FILEBASENAMEASIDENTIFIER___ # pragma mark - LifeCycle - viewDidLoad (void) {[super viewDidLoad];} #pragma mark - UITableViewDelegate #pragma mark - Public #pragma mark - Private #pragma mark - Getter #pragma mark - Setter @end

Then replace the original file with this file. After the successful replacement of a new controller, it will be our custom structure, and if it does not work, you can restart the Xcode Oh:

Xcode custom template
a new controller

I believe that here, other templates you will certainly change it.

Use custom templates

If you do not want to modify the system template, you can create a new Custom Template folder in File Templates, the system’s Cocoa Touch Class.xctemplate copy into, and then modify it. Does not affect the default template style of the system.

So in the creation of the file, in the bottom will have your own template options;

Xcode custom template
select template

Well, here we should be familiar with the basic use of the Xcode template, to explore more advanced usage!

Welcome to me and my topic: iOS technical communication, see more good articles.