// MARK: - What is it?

If you have ever created a new view controller, you might have a chance to see this boilerplate code in the newly created view controller.

If you are coding long enough, you might already know what it is. But for a newcomer, you might wonder what is // MARK: - Navigation doing here.

What is this // MARK #

MARK is one of a code annotation that adds a visual clue to the jump bar and minimap .

The following is an example of how // MARK shows up in the jump bar and minimap.

@UIApplicationMain

class AppDelegate : UIResponder , UIApplicationDelegate {

. . .





func application ( _ application : UIApplication , configurationForConnecting connectingSceneSession : UISceneSession , options : UIScene . ConnectionOptions ) - > UISceneConfiguration {





return UISceneConfiguration ( name : "Default Configuration" , sessionRole : connectingSceneSession . role )

}



. . .

}

The above code will result in a header name "UISceneSession Lifecycle" showing up in jump bar and minimap.

Jump bar

Jump bar pop-up menu when clicking on the final segment in the path

Minimap (on the right)

If you can't find the minimap in your Xcode, make sure you are using Xcode 11 (or higher). Then in the toolbar, click the Editor Options button, then choose Minimap ( ⌃ – Control + ⇧ - SHIFT + ⌘ - CMD + M ). You can access jump bar in three ways Click on the final segment in the path in the jump bar. In the toolbar, click View > Editor > Show Document Items. ⌃ – Control + 6 . (This is my favorite way of open it)

There are some variations of code annotation, I will briefly go through all of them.

Add a heading #

Insert a comment ( // ) with the prefix MARK: followed by your section heading.



override func viewDidLoad ( ) {

super . viewDidLoad ( )





}

MARK in jumpbar

MARK in minimap

Mark in Objective-C #

You can also use #pragma mark and #pragma mark - in Objective-C. Which is equivalent to // MARK: and // MARK: - .

# pragma mark View life cycle

- ( void ) viewDidLoad {

[ super viewDidLoad ] ;



}

Add a separator line #

Throughout the article, you might notice a MARK with and without a hyphen ( - ). This hyphen has a special meaning. If you put a hyphen before the comment part of an annotation, it will add a separator above your annotation in three places: code, jump bar, and minimap.



override func prepare ( for segue : UIStoryboardSegue , sender : Any ? ) {





}

Separator showing in code, jump bar, and minimap

There is also a less less-known variation on the separator. If you put a hyphen after a comment, it will add a separator below the comment.



override func prepare ( for segue : UIStoryboardSegue , sender : Any ? ) {





}

Separator below a comment

If you want just a separator, that also possible with a hyphen without any comment.



override func prepare ( for segue : UIStoryboardSegue , sender : Any ? ) {





}

Separator without comment

There are two more annotations that don't have a visual clue on code or the minimap, but convey special meaning and icon in the jump bar.

Add a to-do item #

There is a // TODO: which will show as a to-do icon in the jump bar.

private func longProcessingTimeFunction ( ) {



}

TODO annotation in a jump bar show as a to-do icon

Add a bug fix reminder #

To add a bug fix reminder, use FIXME . Which will add a shiny band-aid icon in the jump bar. This one is quite standout from other annotations, which is good.

private func absolute ( _ x : Int ) - > Int {



return x

}

FIXME annotation in a jump bar show as a band-aid icon

How to use #

These annotations exist to help you annotate your code. So that you can section and quickly navigation within the file (MARK), pinpoint the remaining tasks (TODO), and remind you for the remaining problems (FIXME).

There is no rule on how to use these, this article shows you all possible ways to annotate your code. How do you use this tool is solely up to you.

Feel free to tweet me your creative way of using these annotations. Right now, I mostly use it for section delegate and data source.

Yes, it also supports emoji

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Tweet

Share

← Home