Xcode and #pragma mark

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.
  1. One thing to note, if in your general preferences for XCode you checked to ‘sort alphabetically’ it will screw up all your pragma notation in the pull down. I struggled trying to figure out why none of my pragma looked like yours … and that was the reason.

  2. where is the Code Sense preference settings at XCode4?? is there such an option?

  3. You should write also:

    #pragma mark – Initialization

    Its the same as:

    #pragma mark –
    #pragma mark Initialization

    The char “-” means only “create a delimiter in dropdown menu”.

    • Agree with this – it only saves one line each time, but is simpler, and has exactly the same effect.

Comments are closed.