TODO FIXME And Compiler Directives

Want to be sure you don’t ship code without doing your TODO’s or fixing your FIXME’s? Take a look at the compiler directives added in Swift 4.2.

Xcode Annotations

Xcode supports a number of annotations to source code that will show up in the navigation jump bar. Here is an example of an annotated view controller:

import UIKit

// FIXME: Leaking memory

// TODO: -
// TODO: Refactor data source and navigation
// TODO: Need to localize strings
// TODO: -

class TaskTableViewController: UITableViewController {

  // MARK: - UITableViewDataSource -
  ...

  // MARK: - Storyboard Seque -
  ...
}

As well as being a visual reminder in the source code Xcode also uses the annotations in the jump bar navigation menu:

Xcode Jump Bar Menu

Note:

  • The TODO and FIXME annotations show up with different icons in the jump bar menu but apart from that mean whatever you want them to.
  • The MARK annotation adds a heading to the jump bar menu.
  • The hyphen (-) adds a separator line. A leading hyphen adds a line above, a trailing hyphen a line below.

Swift Compiler Directives

The Xcode annotations show up in the jump bar menu but not much else. The compiler ignores them so you can ship without doing your TODO’s or fixing your FIXME’s. They also do not show up in command-line builds.

Swift 4.2 added two more compiler directives that go a step further and cause build-time warnings and errors:

#warning("message")
#error("message")

You use them like this:

func viewSetup() {
    #warning("Strings are not localized")
    ...
}

func updateView() {
    #error("Accessing UI on wrong thread")
    ...
}

Compare the warning and error compiler directives to the TODO and FIXME annotations. This time the compiler generates a warning and error message that are visible in the source code editor and will also show up in command-line builds:

Compiler warning and error directives

Much harder to ignore.

Learn More