If you use Swift Package, there are 2 pesky problems whenever you create a new file.

Problem 1: No header template

It creates a new file WITHOUT package name and organization copyrighty stuff.

Oh gosh. How empty.

Solution: Workspace Header Template

This solution only works if you have a workspace containing the Swift Package.

Create IDETemplateMacros.plist and place it in

  • MyModule.xcworkspace/xcshareddata/IDETemplateMacros.plist

A standard plist will look like the following. Change the macros as needed.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
//  ___FILENAME___
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright © ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.

Problem 2: No prompt for filename

With the custom header template, things will look much better. Yet there is one other weird behaviour.

Somehow, only for Swift Package, Xcode will never ask for the filename!

It will be created as “File.swift”. Then we dev have to waste our time renaming the file, and also correct the name in header!

Solution: Xcode Template

The problem lies with Xcode default template. It is configured that way for Swift package files. You can see the template definition in
/Applications/ Templates/MultiPlatform/Source/Swift File.xctemplate/TemplateInfo.plist.

I wrote about custom file templates in 2017.

We can use it to write a Better Swift template.

Create the following directory:

  • ~/Library/Developer/Xcode/Templates/File Templates/Source/Better Swift.xctemplate/

It needs 2 files.

The first is ___FILEBASENAME___.swift, with the content,


import Foundation

That’s how the string from IDETemplateMacros.plist is appended to the template. You can adjust them to your workflow. 🚀

The 2nd file is TemplateInfo.plist, with this content,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
	<string>An empty Swift file.</string>
	<string>An empty Swift file</string>
    <array />
			<string>The name of the file to create</string>

My plist above added the Options key-value (the rest are the same copied from the Xcode template).

With that, whenever you create a new file, choose Better Swift.

If you’re curious how that options change everything, read the post on the productName identifier.




Back to Home