如何在Xcode 4中创建项目模板

时间:2023-01-13 18:11:02

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:

我从以下网站获取了以下答案的大部分信息:

4 个解决方案

#1


75  

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设置项目模板实际上并不困难,但很难在Web上找到将它们放在一起的信息!希望以下步骤可以帮助您创建自己的。

Setting Up

配置

  1. 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/
  2. 让我们首先获取现有Xcode项目模板的副本以用作基础。打开Finder,转到Applications并右键单击Xco​​de以显示包内容。导航到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中,PATH如下:〜/ Library / Developer / Xcode / Templates / Application / Project Templates。如果模板/应用程序/项目模板不存在,您也应该创建它。感谢seufagner在下面的评论中更新,尽管我自己没有尝试过。 [/编辑]

  1. Copy (do not cut/paste!) one of the listed templates.
  2. 复制(不剪切/粘贴!)列出的模板之一。
  3. 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/
  4. 打开另一个finder窗口并导航到以下目录(取消隐藏你的Library文件夹,在终端输入这个cmd:chflags nohidden~ / Library /或选择在点击Finder / Go时按alt / option键)/ Library / Developer / Xcode /模板(您可能需要创建此文件夹,如果尚未存在)/项目模板/
  5. 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. 如何在Xcode 4中创建项目模板
  6. 在这里创建一个文件夹。无论你想要什么,都可以打电话,建议是你的公司名称。此名称显示在Xcode中新项目对话框的左侧菜单中。例如在附图中我称之为我的Appscore,还有另一个用于cocoas2d。
  7. 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.
  8. 将项目模板粘贴到此处并将其文件夹名称更改为您希望的任何内容,例如MySuperProjectTemplate.xctemplate。我们还没有完成,因为我们需要更改模板的标识符。否则它将不会出现在Xcode新项目对话窗口中。
  9. 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.
  10. 在TextEdit中打开TemplateInfo.plist文件。搜索标识符键。您应该看到类似于“com.apple.dt.unit.XXXXXX”的字符串值。只要它的独特之处,用你想要的任何东西替换它。我再次建议添加您的公司名称和描述模板的名称。
  11. If you now open Xcode you should see the project template appearing under your company name in the new project dialogue.
  12. 如果您现在打开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?

目前,您拥有一个不太有用的现有项目模板的副本。我猜你有很多课程可以在几乎所有项目中重复使用?我们如何将它们包含在此模板中?

  1. 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/
  2. 复制您想要的文件,并将它们粘贴到新的项目模板中,即导航到/ Library / Developer / Xcode / Templates / Project Templates // MySuperProjectTemplate.xctemplate /
  3. 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:

    再次打开TextEdit中的TemplateInfo.plist文件。我们要做的第一件事就是告诉项目模板包含新文件,以便搜索一个名为“Nodes”的键,它有一个值数组。添加以下两行:

    <string>_VARIABLE_classPrefix:identifier_.h</string> <string>_VARIABLE_classPrefix:identifier_.m</string>

    _VARIABLE_classPrefix:identifier_.h _VARIABLE_classPrefix:identifier_.m

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>

___ VARIABLE_classPrefix:identifier ___ BaseViewController.h ___ VARIABLE_classPrefix:identifier ___ BaseViewController.m

如何在Xcode 4中创建项目模板

  1. 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).
  2. 接下来找到Definitions键,您应该看到一个字典作为其值。在这里,我们必须添加对包含文件的引用。创建一个新密钥并将其命名为___ VARIABLE_classPrefix:identifier ___ BaseViewController.h(再次以BaseViewController为例)。

如何在Xcode 4中创建项目模板

  1. 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
  2. 此键的值也是字典。它包含一个名为Path的键和一个字符串值,它是文件的名称,例如BaseViewController.h
  3. 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
  4. 我附上以下图片来表明我的意思,因为我认为我的描述不尽如人意。您可能会注意到有一个名为“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.

希望这对某人有用:)任何问题都在下面评论,我会尽力回答。几个星期前我这样做了,所以我的记忆有点朦胧。

#2


5  

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 Mac and

对于Mac和

/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/

for iOS.

对于iOS。

There are few sample templates also on GitHub by Reid Main and another one by Acani. There are also AFNetworking templates, created by Mattt Thompson. See all the examples, including ones built by Apple and then you can start creating your own.

Reid Main的GitHub和Acani的另一个模板也很少。还有由Mattt Thompson创建的AFNetworking模板。查看所有示例,包括Apple构建的示例,然后您就可以开始创建自己的示例。

#3


4  

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

#4


1  

@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.

自定义模板中的值,以便我们可以使用完全空的模板,我们可以在其中执行任何自定义操作。

#1


75  

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设置项目模板实际上并不困难,但很难在Web上找到将它们放在一起的信息!希望以下步骤可以帮助您创建自己的。

Setting Up

配置

  1. 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/
  2. 让我们首先获取现有Xcode项目模板的副本以用作基础。打开Finder,转到Applications并右键单击Xco​​de以显示包内容。导航到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中,PATH如下:〜/ Library / Developer / Xcode / Templates / Application / Project Templates。如果模板/应用程序/项目模板不存在,您也应该创建它。感谢seufagner在下面的评论中更新,尽管我自己没有尝试过。 [/编辑]

  1. Copy (do not cut/paste!) one of the listed templates.
  2. 复制(不剪切/粘贴!)列出的模板之一。
  3. 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/
  4. 打开另一个finder窗口并导航到以下目录(取消隐藏你的Library文件夹,在终端输入这个cmd:chflags nohidden~ / Library /或选择在点击Finder / Go时按alt / option键)/ Library / Developer / Xcode /模板(您可能需要创建此文件夹,如果尚未存在)/项目模板/
  5. 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. 如何在Xcode 4中创建项目模板
  6. 在这里创建一个文件夹。无论你想要什么,都可以打电话,建议是你的公司名称。此名称显示在Xcode中新项目对话框的左侧菜单中。例如在附图中我称之为我的Appscore,还有另一个用于cocoas2d。
  7. 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.
  8. 将项目模板粘贴到此处并将其文件夹名称更改为您希望的任何内容,例如MySuperProjectTemplate.xctemplate。我们还没有完成,因为我们需要更改模板的标识符。否则它将不会出现在Xcode新项目对话窗口中。
  9. 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.
  10. 在TextEdit中打开TemplateInfo.plist文件。搜索标识符键。您应该看到类似于“com.apple.dt.unit.XXXXXX”的字符串值。只要它的独特之处,用你想要的任何东西替换它。我再次建议添加您的公司名称和描述模板的名称。
  11. If you now open Xcode you should see the project template appearing under your company name in the new project dialogue.
  12. 如果您现在打开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?

目前,您拥有一个不太有用的现有项目模板的副本。我猜你有很多课程可以在几乎所有项目中重复使用?我们如何将它们包含在此模板中?

  1. 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/
  2. 复制您想要的文件,并将它们粘贴到新的项目模板中,即导航到/ Library / Developer / Xcode / Templates / Project Templates // MySuperProjectTemplate.xctemplate /
  3. 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:

    再次打开TextEdit中的TemplateInfo.plist文件。我们要做的第一件事就是告诉项目模板包含新文件,以便搜索一个名为“Nodes”的键,它有一个值数组。添加以下两行:

    <string>_VARIABLE_classPrefix:identifier_.h</string> <string>_VARIABLE_classPrefix:identifier_.m</string>

    _VARIABLE_classPrefix:identifier_.h _VARIABLE_classPrefix:identifier_.m

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>

___ VARIABLE_classPrefix:identifier ___ BaseViewController.h ___ VARIABLE_classPrefix:identifier ___ BaseViewController.m

如何在Xcode 4中创建项目模板

  1. 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).
  2. 接下来找到Definitions键,您应该看到一个字典作为其值。在这里,我们必须添加对包含文件的引用。创建一个新密钥并将其命名为___ VARIABLE_classPrefix:identifier ___ BaseViewController.h(再次以BaseViewController为例)。

如何在Xcode 4中创建项目模板

  1. 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
  2. 此键的值也是字典。它包含一个名为Path的键和一个字符串值,它是文件的名称,例如BaseViewController.h
  3. 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
  4. 我附上以下图片来表明我的意思,因为我认为我的描述不尽如人意。您可能会注意到有一个名为“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.

希望这对某人有用:)任何问题都在下面评论,我会尽力回答。几个星期前我这样做了,所以我的记忆有点朦胧。

#2


5  

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 Mac and

对于Mac和

/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/

for iOS.

对于iOS。

There are few sample templates also on GitHub by Reid Main and another one by Acani. There are also AFNetworking templates, created by Mattt Thompson. See all the examples, including ones built by Apple and then you can start creating your own.

Reid Main的GitHub和Acani的另一个模板也很少。还有由Mattt Thompson创建的AFNetworking模板。查看所有示例,包括Apple构建的示例,然后您就可以开始创建自己的示例。

#3


4  

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

#4


1  

@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.

自定义模板中的值,以便我们可以使用完全空的模板,我们可以在其中执行任何自定义操作。