4 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

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

Project user data - will not be shared between project members: <ProjectName>.xcodeproj/xcuserdata/[username].xcuserdatad/IDETemplateMacros.plist.

- will not be shared between project members: Project shared data - will be shared with evderyone working on the same project: <ProjectName>.xcodeproj/xcshareddata/IDETemplateMacros.plist

- will be shared with evderyone working on the same project: Workspace user data - same as above, just for a whole workspace: <WorkspaceName>.xcworkspace/xcuserdata/[username].xcuserdatad/IDETemplateMacros.plist.

- same as above, just for a whole workspace: Workspace shared data - same as above, just for a whole workspace: <WorkspaceName>.xcworkspace/xcshareddata/IDETemplateMacros.plist.

- same as above, just for a whole workspace: User Xcode data - will be used for all projects, is not shared with other developers: ~/Library/Developer/Xcode/UserData/IDETemplateMacros.plist.

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 <plist version= "1.0" > <dict> <key> FILEHEADER </key> <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:

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.