Xcode class templates
For example, creating a UIViewController, a Category, etc., these are actually templates that Xcode helps us integrate.
Custom Xcode class template
When we use the template that comes with Xcode to create a subclass of UIViewController, we will find that the .m file will have some more methods
In our usual development, we have more or less our own specifications, especially in the case of multi-person cooperative development, some good development specifications are quite necessary.
Reference UIViewController
specification:
#import "MyViewController.h"
@interface MyViewController ()
//
@end
@implementation MyViewController
#pragma mark - LifeCyle
- (void)viewDidLoad {
[super viewDidLoad];
}
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
- (void)dealloc {
NSLog(@"%@-释放了",self.class);
}
#pragma mark - Intial Methods
#pragma mark - Target Methods
#pragma mark - Private Method
#pragma mark - Setter Getter Methods
#pragma mark - External Delegate
#pragma mark - UITableViewDelegate,UITableViewDataSource
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return <#NSInteger#>;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#NSInteger#>;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return <#UITableViewCell#>;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return <#CGFloat#>;
}
**/
@end
Create your own Xcode class template
The template structure looks like this:
Introduce what's inside (xxx is the name of your custom template):
xxx.xctemplate
template file.
xxxObjective-C
OC template file, which contains___FILEBASENAME___.h
and___FILEBASENAME___.m
xxxXIBObjective-C
More thanxxxObjective-C
one___FILEBASENAME___.xib
file, can be customized.
xxxSwift
Swift template file, which contains a___FILEBASENAME___.swift
file.
xxxXIBSwift
More thanxxxSwift
one___FILEBASENAME___.xib
file, can be customized.
TemplateIcon.png
The image displayed by the template.
TemplateInfo.plist
Template configuration information.
- Copy Cocoa Class.xctemplate, rename and delete unnecessary files.
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source
- Modify .h to
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// ___COPYRIGHT___
//
___IMPORTHEADER_cocoaSubclass___
@interface ___FILEBASENAMEASIDENTIFIER___ : UIViewController
@end
- To modify
.m
, you only need to add the specifications you need, you can refer to theVC
template.
DATE
Identifies the current time;
FILENAME
full name with file suffix;
FILEBASENAME
The name without the file suffix;
FULLUSERNAME
the current username;
PROJECTNAME
project name;
FILEBASENAMEASIDENTIFIER``VC
class name;
IMPORTHEADER_cocoaSubclass
Imported header files.
- Modified
.swift
to:
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// ___COPYRIGHT___
//
import UIKit
class ___FILEBASENAMEASIDENTIFIER___: UIViewController {
// MARK: - LifeCyle
override func viewDidLoad() {
super.viewDidLoad()
}
deinit {
print("\(object_getClassName(self)) - 释放了!")
}
// MARK: - Intial Methods
// MARK: - Target Methods
// MARK: - Private Method
// MARK: - Setter Getter Methods
// MARK: - External Delegate
}
- New
.xib
, rename___FILEBASENAME___.xib
and modify as shown below:
TemplateInfo.plist
SortOrder
The location of the template in the interface;
Options
Corresponding to the above image selection;
FallbackHeader``.h
imported header files;
RequiredOptions
->cocoaSubclass
Whether to support the selection ofxib``Default
defaulttrue
automatic check;
Values
The name of the custom template (must be consistent);
Suffixes
The default class name of the template, the effect is as shown below.
Finally, copy the created xxx.xctemplate
file directly to the system path and use it directly
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source
Modify system template
After the View is created, the xib file is automatically created
Enter the directory and find the Cocoa Touch Class.xctemplate file
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source
Copy a UIViewObjective-C file and rename it to UIViewXIBObjective-C, then go to another folder with xib, copy a copy of the xib to UIViewXIBObjective-C, and the xib can be modified by yourself. (I only configured Objective-C here, Swift steps are similar)
Find the
TemplateInfo.plist
file in the current directory and modify it as follows:
View
You can choosexib
to recreate the system template
Reference link