简体   繁体   English

如何为Xcode 6创建自定义Swift文件模板?

[英]How to create a custom Swift File template for Xcode 6?

I'd like to make a couple of Swift file templates that I can drop into Xcode's file template picker. 我想制作一些Swift文件模板,我可以将其放入Xcode的文件模板选择器中。 I know how to make a custom Playground template and Obj-C file templates but for Swift there seems to be no file template available at all in the following path: 我知道如何制作自定义Playground模板和Obj-C文件模板,但对于Swift,似乎在以下路径中根本没有可用的文件模板:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/

This surely should be possible, shouldn't it? 这肯定应该是可能的,不应该吗? (Or does every app developer start with the awful default templates?) (或者每个应用程序开发人员都使用可怕的默认模板开始?)

I've created a new template folder under the path 我在路径下创建了一个新的模板文件夹

/Users/name/Library/Developer/Xcode/Templates/File Templates/User Templates

with the name Swift File.xctemplate and the following contents: 名称为Swift File.xctemplate ,内容如下:

___FILEBASENAME___.swift
TemplateIcon.png
TemplateIcon@2x.png
TemplateInfo.plist

and of course edited the files accordingly. 当然还是相应地编辑了文件。 When I open Xcode's temple chooser, my template appears but it somehow doesn't work correctly. 当我打开Xcode的寺庙选择器时,我的模板出现但它不知何故无法正常工作。 It also opens a file browser and asks where to store the file which isn't right, and the file doesn't appear in the project afterwards. 它还会打开一个文件浏览器并询问存储不正确的文件的位置,然后该文件不会出现在项目中。

Is there any solution to this? 这有什么解决方案吗? The web seems to be devoid of such information. 网络似乎没有这些信息。

Using Xcode Version 6.1.1 (6A2008a), if you go into one of the template bundles found in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source you'll see the swift templates. 使用Xcode Version 6.1.1(6A2008a),如果你进入/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source找到的模板包之一你会看到快速的模板。

So, for example, let's open up a new Terminal window and look inside the Cocoa Touch Class.xctemplate bundle to see all the swift templates: 因此,例如,让我们打开一个新的终端窗口,查看Cocoa Touch Class.xctemplate包,看看所有的swift模板:

$ cd "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate"
$ ls *Swift
NSObjectSwift:
___FILEBASENAME___.swift
...

That should give you a good place to start looking so you can figure out what has gone wrong with your custom template. 这应该给你一个开始寻找的好地方,这样你就可以弄清楚你的自定义模板出了什么问题。

A couple things that always help me are: 总是帮助我的一些事情是:

1) Check, double check, and triple check your TemplateInfo.plist file, it's the most important file in the template and the easiest to mess up. 1)检查,仔细检查和三重检查您的TemplateInfo.plist文件,它是TemplateInfo.plist中最重要的文件,最容易搞砸。

2) Sometimes, when I can't get Xcode to recognize my new template, or changes to an existing template, then restarting the dang thing sometimes works wonders. 2)有时,当我无法让Xcode识别我的新模板或更改现有模板时,重新启动dang的东西有时会产生奇迹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM