Swift’s Answer to #pragma mark, FIXME and TODO

While coding in Objective-C, #pragma mark is very handy for code organization in the Jump Bar. For example:

#pragma mark – Initialization 
code here...
#pragma mark – Table Management
more code here...

The Jump Bar would show the following, where code sections are clearly marked:

Swift #pragma alternative // MARK

#pragma mark Alternative for Swift

Xcode 6 now supports a similar feature using // MARK:

// MARK: - Initialization
code here...
// MARK: - View Management
more code here...

With the result being:

// MARK in Xcode 6

The “-” after // MARK: is optional, including the “-” results the divider line shown just above the text.

// TODO: in Swift

Although not used as frequently (at least from my perspective), but handy none-the-less are FIXME and TODO. The later is nice when you need to set a reminder for code that you need to revisit.

override func viewDidLoad()
  // TODO: add configuration code

You can also add TODO: outside a method as shown below:

// TODO: revisit memory management handling
func setupMemoryRecoveryCode()

Notice in the screenshot below that the TODO: references appear at different levels – the first TODO: is indented, indicating it is referencing something to do inside the method itself.

TODO: in Swift Xcode 6

// FIXME: in Swift

// FIXME: works in a similar manner, as it can be place either inside or out of a method.

For example, I’ll often place a // FIXME: with a bug reference once I track down the location of the problem. This gives me a marker and I can quickly return to the issue later.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  // FIXME: bug 2102
  let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
  let object = objects[indexPath.row] as NSDate
  cell.textLabel.text = object.description
  return cell

The output in the Jump Bar looks as follows:

// FIXME: in Xcode 6