Xcode change the description(header) of the file that was just created
Asked Answered
A

2

7

When I create a new file in my project in the file header, I find this:

//
//  NameFile.swift
//  NameProject
//
//  Created by Name Surname on dd/mm/yy.
//

I would like to change it, but in the settings I don't find where to do it.

I would like to change it for all possible future projects.

I would like to achieve such a thing.

//
//  NameFile.swift
//  NameProject
//
//

Edit:

I would like to try to remove the comment, but I can't find solutions.

Alpert answered 16/5, 2022 at 14:2 Comment(4)
Search StackOverflow for the keyword IDETemplateMacros.plist. Also, check out this article: oleb.net/blog/2017/07/xcode-9-text-macrosOpportunist
OK, I think I've found a way by following a tutorial on yt. But is there a way to completely get rid of this header comment?Alpert
As far as I know there isn't, but in my current team we just have a one line template that says // Delete this header and it works ok.Opportunist
This is a real problem.Alpert
L
5

Say you want to modify (or get rid of) the XCode Header comment.

  • First open XCode, Use File > New File... (⌘N) and choose Property List from the file templates.
  • Name it file IDETemplateMacros.plist
  • On the navigator, select the file as right-click Open as source code. Xcode will show us the property file as text. Property files are really just XML files.
  • Copy paste the following content:
<!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>Created for ___PROJECTNAME___ in ___YEAR___
// Using Swift ___DEFAULTTOOLCHAINSWIFTVERSION___</string>
</dict>
</plist>

On the root dict we have added an entry with key FILEHEADER and a two-lines string as a value:

Created for ___PROJECTNAME___ in ___YEAR___
// Using Swift ___DEFAULTTOOLCHAINSWIFTVERSION___

Save the file IDETemplateMacros.plist on the folder:

~/Library/Developer/Xcode/UserData/

That's it, now when creating a new project called MyProject the header will be:

//Created for MyProject in 2022
// Using Swift 5.0

Note1. There is a list of macros on https://help.apple.com/xcode/mac/9.0/index.html?localePath=en.lproj#/dev7fe737ce0

Note 2. As an example you can write:

 Created ___DATE___
// ___COPYRIGHT___

Note that there is a leading space but you do not include the // for the comment on the first line.

Note 3. For a more list of options see: https://useyourloaf.com/blog/changing-xcode-header-comment/

Loar answered 29/6, 2022 at 20:52 Comment(2)
Is there any way to do this on a project level?Schaaf
@Schaaf Yes, it is explainedstep by step in the link of Note 3.Loar
A
1

From https://developer.apple.com/forums/thread/711305?answerId=722311022#722311022

Add ~/Library/Developer/Xcode/UserData/IDETemplateMacros.plist with an empty FILEHEADER entry to generate an empty comment.

<?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></string>
</dict>
</plist>

Xcode help:

If you really want to remove that empty comment line then you will need to start adding custom file templates. eg for a No Comment Swift File template create the following:

  • ~/Library/Developer/Xcode/Templates/File Templates/MultiPlatform/Source/No Comment Swift File.xctemplate/___FILEBASENAME___.swift with import Foundation followed by an empty line
  • ~/Library/Developer/Xcode/Templates/File Templates/MultiPlatform/Source/No Comment Swift File.xctemplate/TemplateInfo.plist with something like

TemplateInfo.plist

<?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>SupportsSwiftPackage</key>
    <true/>
    <key>Kind</key>
    <string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>
    <key>Description</key>
    <string>A no comment Swift file.</string>
    <key>Summary</key>
    <string>A no comment Swift file</string>
    <key>SortOrder</key>
    <string>1</string>
    <key>Image</key>
    <dict>
        <key>FileTypeIcon</key>
        <string>swift</string>
    </dict>
    <key>AllowedTypes</key>
    <array>
        <string>public.swift-source</string>
    </array>
    <key>Platforms</key>
    <array/>
    <key>DefaultCompletionName</key>
    <string>File</string>
    <key>MainTemplateFile</key>
    <string>___FILEBASENAME___.swift</string>
</dict>
</plist>

The default empty Swift File template in the Xcode 14 beta is: /Applications/Xcode-beta.app/Contents/Developer/Library/Xcode/Templates/File Templates/MultiPlatform/Source/Swift File.xctemplate/ (do not edit this directly).

Annihilation answered 1/8, 2022 at 1:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.