Sep 30 2008

Xcode and #pragma mark

Published by at 5:35 am under Xcode

  

I’ve started using #pragma mark directives in my code to help with organization as my implementation files grow. #pragma mark is simple to use, for example, insert the following to call out initialization code:

#pragma mark -
#pragma mark Initialization

Once this is in place, the Functions Menu (in the navigation bar) which shows a list of locations within a source file (e.g. definitions of classes, functions and methods) will display a new marker with the label "Initialization." The code in line 1 will add a line separator inside the Functions Menu, in this example, with the line appearing above the "Initialization" marker.

The figure that follows shows an example of how you might use #pragma mark to divide up various sections of your code.

Two notes:

  1. You cannot have a space after the "-" in the #pragma mark -
  2. If your code does not appear as expected (e.g. the separator does not appear), check that ‘Sort list alphabetically’ is not checked in the Code Sense preference settings.

5 responses so far

5 Responses to “Xcode and #pragma mark”

  1. funkyboyon 30 Sep 2008 at 5:41 am

    Did not know. Good tip! Thanks for sharing!

  2. jesseon 17 Sep 2009 at 10:58 pm

    great explanation. especially the Sort list alphabetically.

  3. Donlyon 16 Nov 2010 at 8:18 pm

    yeah, I got it.

  4. Gauravon 03 Nov 2011 at 3:08 am

    This can also be achieved in X-Code by

    // MARK: -
    // MARK: Testing Mark

    just give it a try. :)

  5. Rahulon 24 May 2012 at 12:07 pm

    Thnx .. good one