How to tag your TODO, FIXME and ERROR in Xcode


This post is a free translation of How to highlight your TODOs, FIXMEs, & ERRORs in Xcode by Hector Matos


It was a very ordinary day: I wrote code, fixed bugs, and in general everything was perfect. It was then that I wrote a block of code that I had to return to later. This is a common case that you probably also encountered: you had to interact with an API that was not yet ready. I knew the general structure of the object that I would get by the API, but I still could not test how to work with it. Like any other developer, I wrote a comment that looks like this:



At this point, I would like to create a warning in Xcode, the same as we used to do in Objective-C using compiler directives:



But alas, that did not work out and I was sad.



As a man of action, I did what followed: I acted. It turns out you can add a run script to get the necessary functionality.


RUN SCRIPT BUILD PHASES


Xcode supports internal bash commands or scripts in various phases of your development cycle. You can run the bash script anytime before or after building, running, testing, profiling, analyzing, or even archiving!


To do this, go to the "Build Phases" of your project in Xcode, click on the "+" at the top left and then select "New Run Script Phase" from the drop-down menu:



Then you will see a new section where you can write a bash script. If you are already an expert on writing Swift scripts after reading the scripting in swift post , you can simply put the script file in the root directory of the project and call it from your new run script.



# Mark your TODO, FIXME, and ERROR with native Xcode warnings


Put this wonderful code in the body of your run script:


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

From now on, you will see warnings when you put the label TODO: or FIXME: in the comment! See how this magic works:



We will not stop there and fix the script in such a way as to highlight errors using // ERROR: in the comments. As you know, there are times when we want to pay special attention, highlighting the error using // the ERROR: . To do this, modify your bash script like this:


TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
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/"

I don’t know about you, but I’m probably the most forgetful person in the world. At the end of the day, my current code fragment is not always finished and I like to use // ERROR: to remind myself of what to work on tomorrow.



When my IDE looks like this , I immediately feel that it is necessary to finish what has already been started. And do not worry, the errors generated by this script do not interfere with the assembly of the project.


CONCLUSION


In your daily work, you will always come across a block of code that you will need to return to later, but now you are forced to put a “patch” and move on. Unfortunately, even a simple // TODO: , // FIXME: or // ERROR: in the comments, just not enough. You'd be surprised how many people forget about their // TODO: , // FIXME: and // ERROR: the project. Using a run script in this situation is a great way to make sure you don't miss out on anything in your development cycle. Hope this helps!


Happy coding, fellow nerds!


P.S. Another modification of the script was extracted from the comments on the original post, in case you still need to prevent the project from being built if there are marks // ERROR: in the code:


TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
OUTPUT=$(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/")
ECHO "$OUTPUT"
if [[ $OUTPUT == *" error: "* ]]
then
exit 1
fi

Also popular now: