Xcode makes its own file template

What is a file template:

The file template is the template used by xcode when the project is created. For example, when creating UIViewController and UITableViewCell files, the file's own content.

Why make your own file template

Standardize development, reduce repetitive work, such as creating each time

When the Xcode code block stores the address of UITableViewCell, it is always inevitable to create new labels, buttons, and imageViews, which is very annoying.

How to create

Xcode original document template

In the /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source, there are many file templates that come with Xcode in the Cocoa Touch Class.xctemplate folder. .
File template
We observe the characteristics of the system file template, create a folder of our own file template under the Source folder, the following picture is the file template I created by myself,
Own file template
and then copy the file template in Cocoa Touch Class.xctemplate to my own folder .

Start editing

I want to modify the file template of UIViewController and find the corresponding folder UIViewControllerObjective-C. I want it to inherit from BaseViewController, and then divide the code area in the .m file and write the Umeng statistical code.

//___FILEHEADER___

___IMPORTHEADER_cocoaTouchSubclass___
#import "BaseViewController.h"
NS_ASSUME_NONNULL_BEGIN

@interface ___FILEBASENAMEASIDENTIFIER___ : BaseViewController

@end


NS_ASSUME_NONNULL_END
//___FILEHEADER___

#import "___FILEBASENAME___.h"

@interface ___FILEBASENAMEASIDENTIFIER___ ()

@end

@implementation ___FILEBASENAMEASIDENTIFIER___

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //开始统计
    [MobClick beginLogPageView:NSStringFromClass([self class])];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //结束统计
    [MobClick endLogPageView:NSStringFromClass([self class])];
}
#pragma mark ---------- layout ----------
-(void)layout{
    
}
#pragma mark ---------- delegate ----------
#pragma mark ---------- event response ----------
#pragma mark ---------- HTTP ----------
#pragma mark ---------- private method ----------
#pragma mark ---------- getter && setter ----------

@end

The following is the modification to UITableViewCell

//___FILEHEADER___

#import "___FILEBASENAME___.h"

@interface ___FILEBASENAMEASIDENTIFIER___ ()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *label1;
@end
@implementation ___FILEBASENAMEASIDENTIFIER___


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark ---------- setter && getter ----------
-(UIImageView *)imgView{
    if(_imgView == nil){
        _imgView = [UIImageView new];
    }
    return _imgView;
}
-(UILabel *)titleLabel{
    if(_titleLabel == nil){
        _titleLabel = [UILabel new];
    }
    return _titleLabel;
}
-(UILabel *)detailLabel{
    if(_detailLabel == nil){
        _detailLabel = [UILabel new];
    }
    return _detailLabel;
}
-(UILabel *)label1{
    if(_label1 == nil){
        _label1 = [UILabel new];
    }
    return _label1;
}
@end

Modify the plist file

Change the SortOrder in TemplateInfo.plist to 0. In order to distinguish it from system files, you can change the TemplateIcon.png picture in the folder to your favorite.
Insert picture description here
Insert picture description here
This custom file template can appear on our xcode panel.
Insert picture description here
You can create your own template file in our usual way~
Insert picture description here

At last

Xcode code block storage address

~/Library/Developer/Xcode/UserData/CodeSnippets Xcode

File template storage location:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

This file needs to be backed up, otherwise it will be gone as soon as xcode is updated

Guess you like

Origin blog.csdn.net/qq_28285625/article/details/103786586