what is the '#pragma '' usage?

Discussion in 'Mac Programming' started by youngplayer, May 22, 2008.

  1. youngplayer macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #1
    #import "MyController.h"

    @implementation MyController



    #pragma mark -
    #pragma mark Startup and Shutdown

    - (id) init
    {
    if (self = [super init])
    {
    _mailboxes = [[NSMutableArray alloc] init];
    }
    return self;
    }

    - (void) dealloc
    {
    [_mailboxes release];

    [super dealloc];
    }



    #pragma mark -
    #pragma mark Simple Accessors

    - (NSMutableArray *) mailboxes
    {
    return _mailboxes;
    }

    - (void) setMailboxes: (NSArray *)newMailboxes
    {
    if (_mailboxes != newMailboxes)
    {
    [_mailboxes autorelease];
    _mailboxes = [[NSMutableArray alloc] initWithArray: newMailboxes];
    }
    }


    @end
    ------------------------------------------------------------------------------------------
    As the above sample code shown, what do the #pragma sentences mean?
    I have read the Programming in Objective-C, but it doesn't make me understood yet. Could someone explain more clearly? Thank you!


    Paragraph in Programming in Objective-C:

    The #pragma Directive
    General Format:



    #pragma text

    This causes the preprocessor to perform some implementation-defined action. For example, under the pragma



    #pragma loop_opt(on)

    causes special loop optimization to be performed on a particular compiler. If this pragma is encountered by a compiler that doesn't recognize the loop_opt pragma, it is ignored.

    The special keyword STDC is used after the #pragma for special meaning. Current supported switches that can follow a #pragma STDC are FP_CONTRACT, FENV_ACCESS, and CX_LIMITED_RANGE.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    #pragma mark directives show up in XCode in the menus for direct access to methods. They have no impact on the program at all.
     
  3. youngplayer thread starter macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #3
    It means that these sentences are used as 'comment'?

    what's the difference between '#pragma mark' and usual comment?
    Why does the author write so?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Did you read what I wrote? At all? It tells XCode (or any other IDE that cares to check) to put an entry in it's drop-down menu for fast access to method definitions. That entry acts as a grouping or similar.
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    I didn't realise there were others apart from Pragma Mark that's interesting.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    There are lots. They are compiler (or pre-processor) specific pragmatic information.

    You can get info on the GCC ones here

    Unrecognised pragmas like mark will be ignored by the compiler so can safely be used for other purposes, such as layout hints for IDEs
     
  7. youngplayer thread starter macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #7
    Thank you! Friend! It sounds wonderful of the usage of '#pragma mark'. I'll try when my Mac machine arrives.

    BTW:where can I get more "#pragma" info for XCode?
     
  8. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #8
    Click on the link in the post you quoted. Xcode uses GCC.
     
  9. prosto macrumors newbie

    Joined:
    Dec 5, 2009
    #9
    Here is it

    From "XCode Workspace Guide": "The Function menu lets you jump to many points in the current file..."
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Seriously, why don't you just type "pragma mark" into the Google box in Safari?

    And seriously, why don't I check whether I am replying to a post from May 2008? :-(
     
  11. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #11
    May 2008? Ha!

    For anyone curious, OpenMP is done almost entirely through pragmas. This way, compilers that don't know what to do with OpenMP can easily leave it out without all of the "ifdef" madness.
     

Share This Page