22 Jan 20204 min read

Creating your own custom file header

Everyone is used to the file header that is provided by default in Xcode and there is rarely a need to change that. Why would anyone need to change it anyway? On the other hand... why is it there anyway? Does it provide any useful information?

Default header

//
//  FILE_NAME.swift
//  MODULE_NAME
//
//  Created by AUTHOR on DD/MM/yyyy.
//  Copyright © yyyy ORGANIZATION. All rights reserved.
//

The default header contains a file's name which does not really matter but might be annoying when you refactor your code or simply rename your file but it does not update the header automatically. We do not like inconsistency so wouldn't it be better if we would just remove this line from there?

The same thing is with the module name, we might rename it in the future and the header will stay outdated.

An author might be quite useful but does it really matter who created the file first? I do not think so. The file might be completely changed during development anyway and there might be no single line of code that the initial creator had written anyway.

Also, a date uses the creator's preferred date format which might be inconsistent among all the developers in the project.

Sometimes, especially if you work as a freelance developer we even cannot use the default one as we are obligated by our client to use a different set of data and the only thing we can easily change is the organization name - through the Inspectors panel (the one on the right in Xcode).

People coming from different platforms, like Android or Flutter might be even surprised that Xcode adds such header by default as it's not a common practice there (correct me if I am wrong, please) to add any header at all.

ADVERTISEMENT
Sponsor Ordinary Coding
Sponsor Ordinary Coding

If you are interested in advertising your application or service on my blog, get in touch with me by email ordinary.coding@protonmail.com. If you are an indie developer you can have your app advertised completely for free!

Custom template

It's been a long time since it's possible to provide a custom template for a header as it's been added in the Xcode 9. I never had a need to change that though until now. The client wants me to remove anything except the Copyright line and since it will be a quite big project I do not want (as probably I won't) remember to fix the header each time I create a new file.

To start using a custom header template we need to create an IDETemplteMacros.plist file in one of the 5 available locations:

In the Xcode official documentation, you can find a list of all available macros that can be used within your custom header. For example, if we would like to add a default Copyright text, we would need to include the proper macro. Below you can find a sample IDETemplteMacros.plist with a custom file header

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>FILEHEADER</key>
    <string>
//  OrdinaryCoding.com
//  ___COPYRIGHT___
//
//  Follow me on Twitter: @artrmzx
//</string>
</dict>
</plist>

The first line is empty as Xcode automatically adds a comment mark to the first one. Once you add a plist file one of the above locations, each file you create now will have your new, custom file header:

//
//  OrdinaryCoding.com
//  Copyright © 2020 Artrmz. All rights reserved.
//
//  Follow me on Twitter: @artrmzx

Conclusion

As you can see, it is quite simple to customize your file headers. Got any preferred headers you usually use or would like to use? I believe some people would love to remove it completely, although there is no way to provide an empty string as Xcode adds comment mark // to the first line by default.

Thank you for reading.