How to Highlight Your TODOs, FIXMEs, & ERRORs In Xcode

It was a day like any other day: Squashing bugs, writing code, and generally being awesome. It was then that I wrote a block of code that needed to be revisited on another day. It's a common scenario you can probably relate to, where I had to interface with an API that wasn't ready/live yet. I knew the general structure of the API's response object, but I had to mock it out since I couldn't test it yet. Like any other developer, I wrote a comment that looked like this:

//TODO: Connect to the actual API when it is ready. 
//Keeping this code the way it is at release time can only mean bad things. 
//Really, really bad things. 
//Like a dragon eating a bunny bad.
let responseObject = Mock.getMockObjectForAPI("NotReadyAPI")

//Do cool things with the responseObject

At this point, I wanted to do a warning in Xcode like we used to do in Objective-C with compiler directives like so:

#warning For the love of God fix this
- (NSInteger)crashingMethod {
    @throw NSInternalInconsistencyException;
    return 0;

But alas, I could not and that made me sad.

Like the man of action I am, I did what you would expect me to do: I DID SOMETHING ABOUT IT. Turns out, you can write a run script to achieve this same functionality for you!


Run Script Build Phases

Xcode supports injecting bash commands or scripts in different phases of your dev cycle. You can run a bash script anytime before or after building, running, testing, profiling, analyzing, or even archiving!

To do this, navigate to your project's build phases in Xcode, click on the + sign towards the top left of the window, and then click on "New Run Script Build Phase" in the drop down menu:

You should then see a new section added where you can inject your bash script. In fact, if you've been going crazy writing Swift scripts after reading my post on scripting in swift, you can place it in your project's root directory and you can execute it from your new run script!


# Marking Your TODO's, FIXME's, & ERROR's With A Legit Xcode Warning

In the body of your run script build phase, copy and paste this lovely bit of code:

find "${SRCROOT}" ( -name ".h" -or -name ".m" -or -name ".swift" -type f) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
From here on out, you should now see warnings when you mark your code with TODO: or FIXME: comment blocks! Here's a screenshot of the magic in action:

To take this even one step further, we can edit this script to also highlight errors using an //ERROR: comment block. As you know, there are situations where we want to step up our warning game and highlight an error by marking our code with //ERRORs. To do this, change your bash script in your run script build phase to say this:

find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" -type f\) -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/"

I don't know about all of you, but I'm possibly the most forgetful person in the world. (BUBBLES EDIT: HE REALLY IS THE MOST FORGETFUL PERSON ON EARTH) When I sign off for the day, I don't always have a completed section of code so I like to use //ERROR:'s in my code to remind what to work on the next day.

When my IDE looks like that after waking up, I feel pretty compelled to get shit done. And don't worry, error's generated using this run script WILL NOT fail your build.



In your everyday work environment you will always come across a section of code that you absolutely need to revisit later but have to temporarily patch up to move on. Unfortunately, even a simple //TODO:, //FIXME:, or //ERROR: in a comment is just not enough. You'd be surprised how many people forget their //TODO:'s, //FIXME:'s, and //ERROR:'s in a project's lifetime. Using this run script in this situation is a great way to ensure you don't miss a thing in your project development cycle. Hope this helps!

Happy coding fellow nerds!