DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Remove Xcode Default Header Comment

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!

Paul Zabelin user avatar by
Paul Zabelin
·
Dec. 21, 15 · Mobile Zone · Tutorial
Like (2)
Save
Tweet
4.51K Views

Join the DZone community and get the full member experience.

Join For Free

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
code style XCode

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is ERP Testing? - A Brief Guide
  • Why I'm Choosing Pulumi Over Terraform
  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club

Comments

Mobile Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo