216

What is the purpose of writing comments in Swift as:

// MARK: This is a comment

When you can also do:

// This is a comment

What does the // MARK achieve?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429

2 Answers2

391

The // MARK: and // MARK: - syntax in Swift functions identically to the #pragma mark and #pragma mark - syntax in Objective-C.

When using this syntax (plus // TODO: and // FIXME:), you can get some extra information to show up in the quick jump bar.

Consider these few lines of source code:

// MARK: A mark comment lives here.

func isPrime(_ value: UInt) -> Bool { return true }

enter image description here

And for reference, the quick jump bar is at the top in Xcode:

enter image description here

It exists mostly to help with quick navigation within the file.

Note that the dash (// MARK: -) causes a nice separation line to show up. Consider this MARK comment:

// MARK: - A mark comment lives here.

enter image description here

The darker gray separator line just above the bolded option in that menu comes from the dash.

Additionally, we can achieve this separator line without a comment by simply not having any text after the dash:

// MARK: -

enter image description here

As mentioned, // TODO: and // FIXME: comments will also appear here.

// MARK: - Prime functions

func isPrime(_ value: UInt) -> Bool {
    // TODO: Actually implement the logic for this method
    return true
}

func nthPrime(_ value: UInt) -> Int {
    // FIXME: Returns incorrect values for some arguments
    return 2
}

enter image description here

  • FIXMEs get a little band-aid icon that help them standout.
  • MARK icon looks like a table of contents
  • TODO icons look more like a checklist

Clicking on any line in the quick jump bar takes you directly to that line in the source code.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Is this functionality specific to Swift/Objective-C or Xcode? – ma11hew28 Jul 07 '19 at 23:32
  • Xcode, presumably. Although, I can easily imagine that other IDEs will potentially exhibit similar behavior. – nhgrif Jul 08 '19 at 14:08
  • You can also put the hyphen after your text, `// MARK: /Delegate impl. -`, to mark the end of a section, or why not both: `// MARK: - Attention! -` – Andreas Jul 12 '20 at 13:07
  • @Andreas hyphen `-` is just a horizontal line. No any additional mark presents the meaning of **the end of section**. – ooops Oct 06 '20 at 17:36
  • @ooops Sorry, I'm not entirely sure what you mean. Yes, it's a horizontal line. – Andreas Oct 06 '20 at 19:17
  • when should I use `///` vs `//` in project? All the Apple's documentation is in `///` – MikeMaus Jul 20 '21 at 10:01
  • `///` turns your comment into doc comments that can show up when that code is called or if you generate doc comments for your Swift code. – nhgrif Aug 19 '21 at 20:27
  • It should be noted that the //MARK comments show up on the minimap – barryalan2633 Apr 23 '22 at 18:14
16

MARK simply adds a visual MARK in the jump bar like this:

ex // MARK: Core Data Stack

enter image description here

Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30