Xcode 擁有一個很好的內(nèi)置模板,創(chuàng)建文件時會在頭部生成一段注釋,包含了文件名、創(chuàng)建者、創(chuàng)建時間、版權等信息,但有時候我們需要根據(jù)不同的需求自定義一套模板。
Xcode 的 iOS 代碼模板
空的Swift
文件的默認模板是這樣的:
//
// FileName.swift
// ProjectName
//
// Created by Your Name on 12/29/15
// Copyright (c) 2015 Company. All rights reserved.
//
import Foundation
在一些開源項目中,我們有時候會看到在源代碼頭部有一大串長長的MIT
協(xié)議聲明,每個文件都有,當然,那些開源大牛們肯定不會傻傻的每個文件復制粘貼,這么麻煩的事情身為一個程序員肯定是忍不了的,一定有一個一勞永逸的辦法,那就是為空的Swift
文件創(chuàng)建自定義模板。
創(chuàng)建模板
Xcode 會在 ~/Library/Developer/Xcode/Templates
尋找自定義模板。
Xcode會把一個目錄當作一個組(group),我們可以在上述目錄下創(chuàng)建一個名為Custom
的組,然后把系統(tǒng)自帶的Swift
模板拷貝進去。
在終端中運行如下命令:
$ mkdir -p ~/Library/Developer/Xcode/Templates/Custom
$ cp -R /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/Source/Swift\ File.xctemplate ~/Library/Developer/Xcode/Templates/Custom/
拷貝完后,我們可以看到有這幾個文件:
$ cd ~/Library/Developer/Xcode/Templates/Custom/Swift\ File.xctemplate
$ ls
TemplateIcon.png TemplateIcon@2x.png
TemplateInfo.plist ___FILEBASENAME___.swift
打開 ___FILEBASENAME___.swift
, 代碼如下:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
import Foundation
這就是我們正在找的模板,把它改成你想要的格式就可以了。
我們加入MIT
協(xié)議聲明,如下:
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//
//
// Copyright (c) ___YEAR___ ___FULLUSERNAME___
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
重啟Xcode,新建文件 File → New ,單擊 Custom ,你可以看到新的模板
Enjoy coding!!!