Skip to content

How to Format Swift Code in Xcode 16 Using swift-format: Tips & Commands

  • by

Swift-format is Xcode 16’s newly built-in tool for automatically formatting Swift code, ensuring consistency and readability across projects. This short guide provides tips on utilising swift-format effectively—from formatting individual files in Xcode to applying team-wide style configurations via the command line.

  • Recursively format all Swift files in a directory and process them in parallel
    Run: swift format . -p -r -i
    Typically useful for initiating the formatter usage for an existing project.
  • Format a file in Xcode
    Keyboard shortcut: Shift+Ctrl+I
  • Dump the default configuration from Xcode
    Run: swift format dump-configuration > swift-format.json
    The configuration will be stored in file swift-format.json.
    Place that file in your project’s root directory and rename it to .swift-format. This ensures that every time you run the formatter from the command line or use the keyboard shortcut in Xcode, the configuration is automatically applied. This is especially useful for maintaining team-wide coding style consistency.

Ref: swift-format repository

Tags:

Leave a Reply