How to create custom Swift template for Xcode 6?

I would like to create a couple of Swift file templates that I can add to the Xcode file template picker. I know how to create a custom playground template and Obj-C file templates, but for Swift there seems to be no file template available at all at the following path:

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

      

It should be possible for sure, right? (Or does every app developer start out with terrible default templates?)

I created a new templates folder in the path

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

      

with a name Swift File.xctemplate

and the following content:

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

      

and of course edited the files accordingly. When I open Xcode temple chooser, my template appears, but it somehow doesn't work correctly. It also opens the file browser and asks where to store the file which is wrong and after that the file does not appear in the project.

Is there any solution? The Internet seems to be devoid of such information.

+3


source to share


1 answer


Using Xcode Version 6.1.1 (6A2008a), if you go into one of the template packages found in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

, you will see quick templates.

So, for example, open a new terminal window and look inside the package Cocoa Touch Class.xctemplate

to see all the quick 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
...

      

This should give you a good place to start your search so you can figure out what went wrong with your custom template.



A few things that always help me:

1) Check, double check and triple check your file TemplateInfo.plist

, this is the most important file in the template and the easiest to use.

2) Sometimes when I can't get Xcode to recognize my new template or modify an existing template, restarting the dang thing sometimes works wonders.

+6


source







All Articles