I answered this question so briefly because there's no way to do this properly.
Of course, since it's software, you can do anything: Starting with Input Manager hacks or other ways of code injection to system wide keyboard interception, you can alter your local system to do anything anytime. You could set up an Applescript folder action (arrgh) or use a launch demon and the FSEvents facility to watch your source code files.
You can also add a couple of scripts to Xcode (user scripts in the menu, script phases in targets, custom Actions in the organizer, there's even the very unknown possibility a startup script), but all these solutions are flawed, since it involves the user or custom setup on the user's machine.
I'm not aware of a solution which simply works after checking out a project from SCM. I believe that there's need for this and similar customization scripts, so I filed a bug (radar 7203835, "Feature: more user script triggers in Xcode workflow"). I did not receive any feedback yet.
Here's the full text of the radar entry:
It would be useful to have more places to run scripts in Xcode.
Examples:
Pre build scripts
Pre build scripts could be used to build prerequisites like *.xcconfig files or config.h headers. This is not possible with a "Run Script Build phases", since dependency tracking takes place before any build phase is triggered.
Post build scripts
Similar to above, but running after the build
finished (including code signing etc).
Useful for additional packaging,
validity checking etc.
Pre/Post SCM Commit scripts.
To check project integrity.
Pre/Post File Save Script.
To check/alter a file before saving. E.g. run cody beautifiers
Custom project actions.
I'm aware of the organizer's ability to define arbitrary actions. But this is a per user feature (not part of the project). I'd like to define actions like build or clean that show up in the build menu and that are part of a project.
I'm using the Google Toolbox For Mac Xcode Plugin, it adds a "Correct whitespace on save" parameter that trim trailing whitespace on save. I missed that a lot from emacs.
Starting from Xcode 4.4 whitespaces will be trimmed automatically by default, unless the line is all whitespace. You can also activate Including whitespace-only lines to fix this, which is not active by default.
Go to Xcode > Preferences > Text Editing > While editing
The best and easy way without using scripts, hacks and much more.
Just go to Find And Replace and press alt/option + space and press space button in the replace bar and then click on replace all.
It will replace the whitespaces with normal spaces and the warning/ error will be gone !!
I find using the new Automatically trim trailing whitespace -> Including whitespace-only lines as suggested by @MartinStolz works well while editing but I sometimes need to do Cmd + a -> Ctrl + i and save the file multiple times with the file in focus when I'm not editing.
In case you want to clean a whole project (excluding .md files) without using scripts, you can also do a Find & Replace -> Regular Expression. Since this technique removes trailing space and tabs for documentation/comments as well, you can also try a negative lookahead for blacklisted characters to filter out single-line comments.
Find all trailing whitespace:
[\t ]+$
Find trailing whitespace without touching single-line comments:
^(?!.*\\\\)[\t ]+$
Replace:
<nothing>
So linting can also be done without swiftlint autocorrect or similar third party solutions.