Highlight Warnings in Xcode

In Swift Development, we usually use a TODO comment as a placeholder for future refactor — with that Xcode will conveniently shows the correspond part in its file structure drop down list as below:

However, Xcode cannot remind us for warning or error messages like this scenario. Previously, #warning or #error comments in Objective-C environment could be highlighted by compiler so that developers will notice. Is there a way for Xcode to achieve similar effects for Swift as those in Objective-C?

The answer is yes. We could add a simple Run Script in Build Phase like this:

find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$|($ERRORTAG).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"| perl -p -e "s/($ERRORTAG)/ error: \$1/"

With this script, after re-building your project, all comments with “TODO:”, “FIXME:”, or “WARNING:” as prefix are treated as warnings of the compiler, and the “ERROR:”’s one is regarded as an error.

You could also custom-define this script to make different kinds of messages show up as compiler warnings or errors. In that case developers won’f forget any edge case and the run script will ensure those messages are going to be fixed in one day.

P.S. Hector is the first one shared this feature online, and his article is here.