在 Xcode 中更改模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change templates in Xcode
提问by Misha M
How would I change the initial templates created by Xcode when creating a new Cocoa Class.
创建新的 Cocoa 类时,我将如何更改由 Xcode 创建的初始模板。
I am referring to the comments and class name created when using Xcode's new class wizard.
我指的是使用 Xcode 的新类向导时创建的注释和类名。
采纳答案by Chris Hanson
You wouldn't change the existing templates. In other words, don't modifyanything under the /Developer
hierarchy (or wherever you installed your developer tools).
您不会更改现有模板。换句话说,不要修改/Developer
层次结构下的任何内容(或安装开发人员工具的任何地方)。
Instead, clone the templates you want to have customized variants of. Then change their names and the information in them. Finally, put them in the appropriate location in your account's Library/Application Support
folder, specifically:
相反,克隆您想要自定义变体的模板。然后更改他们的姓名和其中的信息。最后,将它们放在您帐户Library/Application Support
文件夹中的适当位置,特别是:
- File templates:
~/Library/Application Support/Developer/Shared/Xcode/File Templates/
- Target templates:
~/Library/Application Support/Developer/Shared/Xcode/Target Templates/
- Project templates:
~/Library/Application Support/Developer/Shared/Xcode/Project Templates/
- 文件模板:
~/Library/Application Support/Developer/Shared/Xcode/File Templates/
- 目标模板:
~/Library/Application Support/Developer/Shared/Xcode/Target Templates/
- 项目模板:
~/Library/Application Support/Developer/Shared/Xcode/Project Templates/
That way they won't be overwritten when you install new developer tools, and you can tweak them to your heart's content.
这样,当您安装新的开发人员工具时,它们不会被覆盖,您可以根据自己的喜好调整它们。
UpdateFor newer versions of Xcode the updated path will be:
~/Library/Developer/Xcode/Templates/File Templates/Source
更新对于较新版本的 Xcode,更新的路径将是:
~/Library/Developer/Xcode/Templates/File Templates/Source
回答by pconcepcion
This may be useful for somebody:
这可能对某人有用:
As for XCode 6to XCode 9the file templates are in:
至于XCode 6到XCode 9,文件模板在:
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source
Update:As @carbo18 noted, it's proably better to create the directory ~/Library/Developer/Xcode/Templates/File Templates/Source
and put your custom templates there. The best way may be to use as base one of the ones in /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source
更新:正如@carbo18 所指出的,创建目录~/Library/Developer/Xcode/Templates/File Templates/Source
并将您的自定义模板放在那里可能会更好。最好的方法可能是使用其中的一个作为基础/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source
回答by Barry Wark
Xcode uses template files for file and project templates and does variable expansion in both at creation time.
Xcode 将模板文件用于文件和项目模板,并在创建时对两者进行变量扩展。
Xcode 3.0 templates can be found in [Dev Installation]/Library/Xcode/, likely /Developer/Library/Xcode. If you want to modify these templates or add your own, use the following directories to save your new/modified templates so that they are not wiped out by future Developer Tool upgrades:
Xcode 3.0 模板可以在 [Dev Installation]/Library/Xcode/ 中找到,可能是 /Developer/Library/Xcode。如果您想修改这些模板或添加您自己的模板,请使用以下目录来保存您的新/修改过的模板,以便它们不会被未来的开发者工具升级所消灭:
- File templates: ~/Library/Developer/Shared/Xcode/File Templates/
- Target templates: ~/Library/Developer/Shared/Xcode/Target Templates/
- Project templates: ~/Library/Developer/Shared/Xcode/Project Templates/
- 文件模板:~/Library/Developer/Shared/Xcode/File Templates/
- 目标模板:~/Library/Developer/Shared/Xcode/Target Templates/
- 项目模板:~/Library/Developer/Shared/Xcode/Project Templates/
I think that you can also use the /Library/Developer/Shared/Xcode/[File|Target|Project] Templates/ directory for templates shared by all users.
我认为您还可以将 /Library/Developer/Shared/Xcode/[File|Target|Project] Templates/ 目录用于所有用户共享的模板。
If you just want to change the MyCompanyNamein the templates, the following command line will do the trick:
如果您只想更改模板中的MyCompanyName,可以使用以下命令行:
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ "ORGANIZATIONNAME" = "NewCompanyName";}'
A good tutorial on writing file templates is here[MacResearch.org].
关于编写文件模板的一个很好的教程在这里[MacResearch.org]。
回答by v01pe
In Xcode 4 and Xcode 5 the userfile templates can be placed at:~/Library/Developer/Xcode/Templates/[Category]
在 Xcode 4 和 Xcode 5 中,用户文件模板可以放在:~/Library/Developer/Xcode/Templates/[Category]
[Category] can be used to categorize your templates (choose a name of your choise)
[Category] 可用于对您的模板进行分类(选择您选择的名称)
If the folder doesn't exist already, create it!
如果该文件夹不存在,请创建它!
回答by carbo18
As of Xcode 7
从 Xcode 7 开始
- File templates:
~/Library/Developer/Xcode/Templates/File Templates
- Project templates:
~/Library/Developer/Xcode/Templates/Project Templates
- 文件模板:
~/Library/Developer/Xcode/Templates/File Templates
- 项目模板:
~/Library/Developer/Xcode/Templates/Project Templates
For example if I place a Empty Application.xctemplate
in ~/Library/Developer/Xcode/Templates/Project Templates/Application
例如,如果我Empty Application.xctemplate
在~/Library/Developer/Xcode/Templates/Project Templates/Application
回答by tanyanzhi
In xcode 5.0.1:
在 xcode 5.0.1 中:
1.Go->Applications
1.Go->应用程序
2.right click "xcode" application
2.右键单击“xcode”应用程序
3.chose "Show Package Contents"
3.选择“显示包裹内容”
4.contents/Developer/Library/Xcode/Templates
4.contents/Developer/Library/Xcode/Templates
回答by Fattie
2019 quick-and-ready no-nonsense version
2019 速成正版
No need to quit Xcode, you can do this while it is running:
无需退出 Xcode,您可以在它运行时执行此操作:
cd "/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Swift File.xctemplate"
cd "/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Swift File.xctemplate"
presented as one line so you can copy easily from this web page
显示为一行,以便您可以轻松地从此网页复制
do an ls
, and then
做一个ls
,然后
sudo chmod oug+rw ___FILEBASENAME___.swift
you'll enter your password
你会输入你的密码
and then
进而
nano ___FILEBASENAME___.swift
add for example import UIKit
.
添加例如import UIKit
。
or add for example class ___FILEBASENAME___:
或添加例如 class ___FILEBASENAME___:
Save.
节省。
You're done.
你完成了。
Sure, this gets scrubbed on Xcode install, but it actually modifies the "main" Swift button when you click to make a file in Xcode. Enjoy.
当然,这在 Xcode 安装时会被清除,但是当您单击以在 Xcode 中创建文件时,它实际上会修改“主”Swift 按钮。享受。
This is the only reasonable solution which works.
这是唯一可行的合理解决方案。
It takes 2 seconds to re-do it when you update Xcode.
更新 Xcode 时需要 2 秒才能重新执行。
回答by Alex Cio
Right click on xCode and select Show Package contents, then go to contents/Developer/Library/Xcode/Templates
. Here you can find the templates for all programming languages.
右键单击 xCode 并选择Show Package contents,然后转到contents/Developer/Library/Xcode/Templates
. 在这里您可以找到所有编程语言的模板。
Here some visualization:
这里有一些可视化:
回答by Manav
回答by ChrisJF
If you are simply looking to change the Author Nameand Organizationsee this answer.
如果您只是想更改作者姓名和组织,请参阅此答案。
It's much easier than modifying the templates.
这比修改模板容易得多。