一、前序
不管你是新手還是老鳥,開發(fā)中都要?jiǎng)?chuàng)建項(xiàng)目和創(chuàng)建文件,例如:
創(chuàng)建工程 ->選擇模版 ->新建項(xiàng)目

打開工程,新建文件

每次都要從一個(gè)空項(xiàng)目或者空文件開始,自己總結(jié)的一些公用方法都要每一次的拷貝進(jìn)工程,這樣的反反復(fù)復(fù)做同樣的事情對(duì)于程序員來(lái)說是煎熬的,所以就有了自定義工程模版和自定義文件模版的方法(當(dāng)然也可以把一些公用類打包成framework,但這樣解決了文件模版的問題,還沒有解決工程模版的問題),下面開始吧!
二、自定義工程模板
如果從頭構(gòu)建一個(gè)工程模版,那是很麻煩的,不過有便捷的方法,就是改系統(tǒng)模版:
Xcode系統(tǒng)模板的路徑是/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/,文件夾里面有文件模板File Templates和工程模板Project Templates,
到系統(tǒng)模版目錄下(Project Templates -> Application),拷貝Single View Application.xctemplate文件夾到自定義模版目錄,但目前自定義模版文件夾(Templates)中沒有ProjectTemplates文件夾,不過我們可以自己創(chuàng)建一個(gè),然后模擬系統(tǒng)模版目錄結(jié)構(gòu),再Project Templates文件夾下的iOS文件夾中新建ZHJ_Application文件夾,并把Single View Application.xctemplate放到ZHJ_Application文件夾中,并改名為ZHJ View Application.xctemplate

打開ZHJ View Application.xctemplate文件夾,可以看到文件夾中有4個(gè)文件:

TemplateInfo.plist 屬性模板屬性設(shè)置
TemplateIcon.tiff 新建項(xiàng)目時(shí)候模版圖標(biāo)(可以不添加)
Main_iPhone.storyboard、Main_iPad.storyboard:模版中使用。
其中TemplateInfo.plist是模版的屬性文件,每個(gè)模版都一個(gè)自己唯一標(biāo)識(shí),打開TemplateInfo.plist,修改key值Identifier對(duì)應(yīng)的Value為com.apple.dt.unit.ZHJViewApplication,然后保存:

這樣重新打開Xcode,新建項(xiàng)目,發(fā)現(xiàn)我們自定義(改裝Single View模版)的工程模版了:

已經(jīng)成功一半了,但不要忘記我們自定義工程模版的目的是什么。下一步,給工程模版中添加我們自己總結(jié)的一些常用公用類,這樣便于以后新建工程的時(shí)候,不再需要手動(dòng)的拷貝大量文件到新建的工程:
下面以 iOS開發(fā)~設(shè)備信息 提到的@interface UIDevice (Info)類別作為模版的文件為例說明具體如何操作:
1)在自定義模版ZHJViewApplication.xctemplate文件夾中,新建UIDeviceInfo文件夾,把UIDevice+Info.h 及 UIDevice+Info.m放入U(xiǎn)IDeviceInfo文件夾中
2)配置TemplateInfo.plist

添加系統(tǒng)庫(kù):TemplateInfo.plist添加一個(gè)新的Row

然后打開Xcode,新建項(xiàng)目,模版選擇ZHJViewApplication,效果如圖:

注意:TemplateInfo.plist其他配置補(bǔ)充
1)Ancestors:父模版,從父模版那里繼承一些模板的基礎(chǔ)屬性,可以有多個(gè)父類。
系統(tǒng)模版Empty Application.xctemplate:
2)Concrete:設(shè)置為YES的模板才可以顯示在new project的dialog中,此時(shí)這個(gè)模板不能被其他模板繼承。
3)Definitions:將Nodes中定義的文件添加到項(xiàng)目中。
4)Description:新建工程時(shí),模版的描述。
5)Identifier:模板的唯一標(biāo)示符。
6)Kind:項(xiàng)目模板為Xcode.Xcode3.ProjectTemplateUnitKind,所有工程模版都是這個(gè)值。
7)Nodes:定義添加到工程模版中的文件。
8)Options:定義新建工程中的storyboard等。
工程模版到這里已經(jīng)基本的介紹完了。
三、自定義文件模板
Xcode系統(tǒng)模板的路徑是/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/,文件夾里面有文件模板File Templates和工程模板Project Templates,如果修改UIViewController模板路徑如下:

FlyElephant.png
選擇UIViewController文件夾下的模板文件:

代碼自定義模板:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
#import "___FILEBASENAME___.h"
@interface ___FILEBASENAMEASIDENTIFIER___ ()
@end
@implementation ___FILEBASENAMEASIDENTIFIER___
#pragma mark - LifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Public
#pragma mark - Private
#pragma mark - Accessors
#pragma mark - Request
@end
新建控制器:

創(chuàng)建完成之后模板:
//
// FEViewController.m
// DynamicDemo
//
// Created by keso on 2017/4/22.
// Copyright ? 2017年 FlyElephant. All rights reserved.
//
#import "FEViewController.h"
@interface FEViewController ()
@end
@implementation FEViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Public
#pragma mark - Private
#pragma mark - Accessors
#pragma mark - Request
@end
同時(shí)可以自定義設(shè)置模板,在文件模板下新建自定義文件夾,然后將Cocoa Touch Class.xctemplate拷貝進(jìn)入新建文件下:

創(chuàng)建工程文件

結(jié)束語(yǔ)
到這里就結(jié)束了,如若不懂的話可以??留言,也可以加入群討論
喜歡的話 記得關(guān)注、收藏、點(diǎn)贊喲