Over a million developers have joined DZone.

Remove Xcode Default Header Comment

Learn all about removing Xcode defaulter header comments, complete with removal of top comments, changing Xcode templates to remove header comments, and more!

· Mobile Zone

Xcode File Template

Every new source file created from Xcode template has top comment similar to this:

//
//  <file name>
//  <Name of project>
//
//  Created by <My name> on <Date>.
//  Copyright <Year and company>. All rights reserved.
//

It has little useful information, and often incorrect. As file name, project and company changes how can we keep in sync file comments? In Agile environment when we pair on shared computers the author name often set to generic ‘Developer’. We also use version control system that can tell us the date when file was created along with all history of modifications. Let’s just get rid of those boiler plate comments and have extra 7 lines on screen to see source code.

Remove Top Comments from All Sources

To clean up your Xcode project from boiler plate comments use Find and Replace. Within Xcode Find the following regular expression and replace it with empty string:

//\n//.+\n//.+\n//\n//.+\n// Copyright.+$\n//\n\n

This will also get rid empty line after the header and allow the source to be the first line in the files.

Change Xcode Templates to Have No Header Comments

Here is a bash script I found thanks to Manav Rathi. You can save it in your home directory, follow the instructions in comments and delete after use.

#!/bin/bash
# Usage: 
# $ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates
# $ bash ~/remove-boilerplate-comments-from-xcode-templates.sh
# Repeat for /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates

find -E . -type f \
    \( -regex '.*\.[chm]' -or -regex '.*\.swift' \) \
    -exec sed -i '' '1,/^$/d' '{}' ';'

We have to do this on all workstations for every Xcode installation.

Git Pre-Commit Hook

To prevent accidental checkin of sources with boilerplate comments we can create git pre-commit hook using overcommit. See earlier post Xcode project git hooks on how to use overcoomit with Xcode.

PreCommit:
  NoXcodeTemplateComments:
    description: 'Checking for Xcode template comments'
    enabled: true
    quiet: true
    exclude:
      - 'Pods/**/*.*'
    include:
      - '**/*.h'
      - '**/*.m'
      - '**/*.c'
      - '**/*.cpp'
      - '**/*.swift'    
module Overcommit::Hook::PreCommit
  class NoXcodeTemplateComments < Base
    def run
      errors = []
      boilerplate = ['Created by', 'Copyright']
      applicable_files.each do |file|
        File.open(file, 'r').each_with_index do |line, index|
          break if index > 10
          boilerplate.each do |text|
            comment = "// #{text} "
            if line.start_with?(comment)
              relative = Pathname(file).relative_path_from(Pathname(Overcommit::Utils.repo_root))
              errors << "#{relative}:#{index} starts with #{comment}"
            end
          end
        end
      end

      return :fail, errors.join("\n") if errors.any?

      :pass
    end
  end
end
Topics:
xcode ,template

Published at DZone with permission of Paul Zabelin. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}