简体   繁体   中英

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

/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:

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

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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