ios 如何在 Xcode 4 中创建项目模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15491826/
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
How to create project templates in Xcode 4
提问by bennythemink
Its really hard to find a tutorial to do this as most information relates to Xcode 3. Answer below may help some people.
很难找到一个教程来做到这一点,因为大多数信息都与 Xcode 3 相关。下面的答案可能对某些人有所帮助。
I sourced most of my information for the answer below from the following sites:
我从以下网站获取了以下答案的大部分信息:
回答by bennythemink
I spent hours searching the web to find information about doing this. Its not actually that hard to set up project templates for Xcode4.5 but its hard to find information on the web that puts it all together! Hopefully the steps below will help you to create your own.
我花了几个小时在网上搜索以查找有关执行此操作的信息。为 Xcode4.5 设置项目模板实际上并不难,但很难在网络上找到将它们放在一起的信息!希望下面的步骤可以帮助您创建自己的。
Setting Up
配置
- Lets start by getting a copy of an existing Xcode project template to use as a base. Open finder, go to Applications and right click on Xcode to show package contents. Navigate to Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Application/
- 让我们首先获取现有 Xcode 项目模板的副本以用作基础。打开 finder,转到 Applications 并右键单击 Xcode 以显示包内容。导航到 Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Application/
[EDIT] In Xcode 5 the PATH is as follows: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists you should create it too. Credit to seufagner in the comments below for the update although I have not tried this myself. [/EDIT]
[编辑] 在 Xcode 5 中,路径如下:~/Library/Developer/Xcode/Templates/Application/Project Templates。如果模板/应用程序/项目模板不存在,您也应该创建它。尽管我自己没有尝试过,但请在下面的评论中感谢 seufagner 进行更新。[/编辑]
- Copy(do not cut/paste!) one of the listed templates.
- Open another finder window and navigate to the following directory (to unhide your Library folder type this cmd in terminal: chflags nohidden ~/Library/ or select press the alt/option key when clicking on Finder/Go) /Library/Developer/Xcode/Templates (you may need to create this folder if not already present) /Project Templates/
- Create a folder in here. Call it whatever you wish, a suggestion is your company name. This name appears in the left hand menu of the new project dialogue in Xcode. e.g. in the attached image I've called mine Appscore, there's another one there for cocoas2d.
- Paste the project template in here and change its folder name to whatever you wish e.g. MySuperProjectTemplate.xctemplate. We are not done yet though as we need to change the template's identifier. Otherwise it will not appear in the Xcode new project dialogue window.
- Open the TemplateInfo.plist file in TextEdit. Search for the Identifier key. You should see a string value something similar to "com.apple.dt.unit.XXXXXX". Replace this with whatever you wish as long as its unique. I again suggest adding your company name and a name that describes the template.
- If you now open Xcode you should see the project template appearing under your company name in the new project dialogue.
- 复制(不要剪切/粘贴!)列出的模板之一。
- 打开另一个 finder 窗口并导航到以下目录(要取消隐藏您的 Library 文件夹,请在终端中键入此 cmd:chflags nohidden ~/Library/ 或在单击 Finder/Go 时选择按 alt/option 键)/Library/Developer/Xcode/模板(如果此文件夹尚未存在,您可能需要创建)/Project Templates/
- 在这里创建一个文件夹。随意称呼它,建议是您的公司名称。此名称出现在 Xcode 中新项目对话框的左侧菜单中。例如,在我称为我的 Appscore 的附加图像中,还有另一个用于 cocoas2d。
- 将项目模板粘贴到此处并将其文件夹名称更改为您希望的任何名称,例如 MySuperProjectTemplate.xctemplate。我们还没有完成,因为我们需要更改模板的标识符。否则它不会出现在 Xcode 新项目对话窗口中。
- 在 TextEdit 中打开 TemplateInfo.plist 文件。搜索标识符键。您应该会看到类似于“com.apple.dt.unit.XXXXXX”的字符串值。只要它是独一无二的,就用你想要的任何东西替换它。我再次建议添加您的公司名称和描述模板的名称。
- 如果您现在打开 Xcode,您应该会在新项目对话框中看到出现在您公司名称下的项目模板。
Customising
定制
At the moment you have a copy of an existing project template which is not very useful. I'm guessing you have a number of classes that get reused in nearly all your projects? How about we include them into this template?
目前,您拥有一份现有项目模板的副本,这不是很有用。我猜你有很多类可以在你几乎所有的项目中重用?我们将它们包含在这个模板中怎么样?
- Copy the files you wish, and paste them into your new project template i.e. navigate to /Library/Developer/Xcode/Templates/Project Templates//MySuperProjectTemplate.xctemplate/
Open the TemplateInfo.plist file in TextEdit again. First thing we have to do is tell the project template to include the new files so search for a key called "Nodes" that has an array of values. Add the two following lines:
<string>_VARIABLE_classPrefix:identifier_.h</string> <string>_VARIABLE_classPrefix:identifier_.m</string>
- 复制你想要的文件,并将它们粘贴到你的新项目模板中,即导航到 /Library/Developer/Xcode/Templates/Project Templates//MySuperProjectTemplate.xctemplate/
再次在 TextEdit 中打开 TemplateInfo.plist 文件。我们要做的第一件事是告诉项目模板包含新文件,因此搜索一个名为“Nodes”的键,其中包含一个值数组。添加以下两行:
<string> _VARIABLE_classPrefix:identifier_.h</string> <string> _VARIABLE_classPrefix:identifier_.m</string>
For example if your controller was called BaseViewController the lines would look like:
例如,如果您的控制器名为 BaseViewController,则行将如下所示:
<string>___VARIABLE_classPrefix:identifier___BaseViewController.h</string> <string>___VARIABLE_classPrefix:identifier___BaseViewController.m</string>
<string>___VARIABLE_classPrefix:identifier___BaseViewController.h</string> <string>___VARIABLE_classPrefix:identifier___BaseViewController.m</string>
- Next find the Definitions key and you should see a dictionary as its value. In here we have to add a reference to the included files. Create a new key and call it ___VARIABLE_classPrefix:identifier___BaseViewController.h (again taking the BaseViewController as an example).
- 接下来找到 Definitions 键,你应该看到一个字典作为它的值。在这里,我们必须添加对包含文件的引用。创建一个新的键并将其命名为 ___VARIABLE_classPrefix:identifier___BaseViewController.h(再次以 BaseViewController 为例)。
- The value of this key is again a dictionary. It contains a key called Path and a string value which is the name of the file e.g. BaseViewController.h
- I've attached the following images to show what I mean as I think my description is falling short. You may notice that there is a key in there called "Group", this as you can guess allows you to create groups and input files directly :D
- 这个键的值又是一个字典。它包含一个名为 Path 的键和一个字符串值,它是文件的名称,例如 BaseViewController.h
- 我附上了以下图片来说明我的意思,因为我认为我的描述不够完善。您可能会注意到其中有一个名为“Group”的键,您可以猜到它允许您直接创建组和输入文件:D
Hope this is actually useful to someone :) Any questions comment below and I'l do my best to answer. I did this a few weeks ago so my memory is a little hazy.
希望这对某人实际上有用:) 任何问题都在下面评论,我会尽力回答。几周前我做了这个,所以我的记忆有点模糊。
回答by Legoless
You can also learn a lot from inspecting the existing project templates, which you can find in:
您还可以从检查现有项目模板中学到很多东西,您可以在以下位置找到这些模板:
/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/
for Macand
对于Mac和
/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/
for iOS.
对于iOS。
There are few sample templates also on GitHubby Reid Mainand another one by Acani. There are also AFNetworkingtemplates, created by Mattt Thompson. See all the examples, including ones built by Apple and then you can start creating your own.
也有少数上示例模板的GitHub由里德主,另一个由Acani。也有AFNetworking模板,通过创建马特·汤普森。查看所有示例,包括 Apple 构建的示例,然后您可以开始创建自己的示例。
回答by bvmobileapps
A good overview of all of the variables used in the plist file can be found here: https://gist.github.com/shazron/943736
可以在此处找到 plist 文件中使用的所有变量的完整概述:https: //gist.github.com/shazron/943736
回答by Pranav Khandelwal
@bennythemink Your answer is really very much help full but I want to add some more things in your answer. When we copy xcode predefine template than this key contains
@bennythemink 您的回答真的很有帮助,但我想在您的回答中添加更多内容。当我们复制 xcode 预定义模板时,该键包含
<key>Ancestors</key>
<array>
<string>com.apple.dt.unit.storyboardApplication</string>
</array>
this value. When we use this key value in Custom template of xcode it will create AppDelegate class by default. Which we can not move to folder only we can shift it into group. So i suggest to add this:
这个值。当我们在 xcode 的自定义模板中使用这个键值时,它会默认创建 AppDelegate 类。我们不能移动到文件夹,只有我们可以将它移到组中。所以我建议添加这个:
<key>Ancestors</key>
<array>
<string>com.apple.dt.unit.objectiveCApplication</string>
<string>com.apple.dt.unit.iPhoneBase</string>
<string>com.apple.dt.unit.prefixable</string>
</array>
value in Custom template so that we will have fully empty template in which we can do any custom stuff.
自定义模板中的值,以便我们将拥有完全空的模板,我们可以在其中执行任何自定义操作。