Automatically Generating Warnings for TODOs in Xcode

When using Objective-C in Xcode, it is possible to generate a compile time warning by using the #warning preprocessor directive in your code.

This can be useful so everything marked as TODO in your code is flagged as a warning when you compile.

Unfortunately, this is not available when using Swift. However, by using a Run Script build phase, you can easily emulate this behaviour.

To do so, do the following:

  1. Select your app target in the project overview.
  2. Click on the Build Phases tab.
  3. Click on + in the top-left to add a new phase.
  4. Select New Run Script Phase.

In the allocated space for the script, enter the following:

TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 \
    | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" \
    | perl -p -e "s/($TAGS)/ warning: \$1/"

This should look similar to the following:

Now when you attempt to build your project, use either of the following to comment your code:

// TODO: Here's something you need to do
// FIXME: Here's something you need to fix

Now when you to build, you will see something similar to the following: