Skip to content

Instantly share code, notes, and snippets.

@Jeehut
Last active May 10, 2019 05:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeehut/e369d91e600ddf62da1c3825301e2f33 to your computer and use it in GitHub Desktop.
Save Jeehut/e369d91e600ddf62da1c3825301e2f33 to your computer and use it in GitHub Desktop.
Migrating from default Xcode headers to shortened ones.

Migrating from Default Xcode headers to shortened ones

By default, Xcode creates comments like this:

//
//  AppDelegate.swift
//  HeaderDemo
//
//  Created by Cihat Gündüz on 09.05.19.
//  Copyright © 2019 Jamit Labs GmbH. All rights reserved.
//

Much of the information is redundant (like the file name) or is inaccurate with the Git history providing more accurate info (contributors). Therefore it can be considered a best practices to use a header comment like this instead:

// Copyright © 2019 Jamit Labs GmbH. All rights reserved.

In order to move a project from the old format to the new one, you need to do two things:

  1. Use Search & Replace in Xcode to fix all existing headers in your project
  2. TEach Xcode how to create future files in your project using the new format

Search & Replace existing headers with new format

  1. Open your project in Xcode
  2. Open the Find navigator on the left pane (the magnifier icon)
  3. Choose "Replace" and "Regular Expression" as options
  4. Copy the following into the upper text field:
(?://[^\n]*\n)+//  Created by[^\n]+\n//  Copyright([^\n]+)\n//
  1. Click enter to start the search
  2. Copy the following into the lower text field:
// Copyright$1
  1. Click "Replace All" to replace all entries at once

Teach Xcode to create future files in new format

  1. Open the root directory of your project in Finder
  2. Right-click your xcodeproj file and choose "Show package contents"
  3. Create (if it doesn't exist already) and open the folder xcshareddata
  4. Create (if it doesn't exist already) and open the file IDETemplateMacros.plist
  5. Copy the following contents to the file and save it:
<?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> ___COPYRIGHT___</string>
</dict>
</plist>

Note that the FILEHEADER keys string value is what Xcode is using then creating a new file. We're basically setting it to ___COPYRIGHT___.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment