What are Objective-C #pragma marks?

#pragma mark -

This is actually an instruction to the IDE. It inserts a separator line into the navigation menu at that point, and also optional header text. It just makes it easier to find your way around a piece of code.

Example:

  1. @interface GCStateMachine : NSObject 
  2.  
  3. @property (nonatomic, readonly) NSSet* states; 
  4. @property (nonatomic, readonly) GCState* currentState; 
  5.  
  6. + (GCStateMachine*) stateMachineWithStates:(NSArray*) states; 
  7.  
  8. - (instancetype) initWithStates:(NSArray*) states; 
  9. - (void) enterState:(Class) stateClass; 
  10. - (void) updateWithDeltaTime:(NSTimeInterval) dt; 
  11. - (GCState*) stateForClass:(Class) stateClass; 
  12. - (BOOL) canEnterState:(Class) stateClass; 
  13.  
  14. @end 
  15.  
  16. #pragma mark - 
  17.  
  18. @interface GCState : NSObject 
  19.  
  20. @property (nonatomic, readonly) GCStateMachine* stateMachine; 
  21.  
  22. + (GCState*) state; 
  23.  
  24. - (BOOL) isValidNextState:(Class) state; 
  25. - (void) didEnterWithPreviousState:(GCState*) previousState; 
  26. - (void) willExitWithNextState:(GCState*) nextState; 
  27. - (void) updateWithDeltaTime:(NSTimeInterval) dt; 
  28.  
  29. @end 

Here I declare two classes in one header file (I usually do this if the two classes are intimately bound to each other and are never used independently). The #pragma mark - between the two makes the navigation menu for this file look like this:

main-qimg-75652ceca199357b025eee1305ad2744.webp

Note the separator line. This is the menu I use most of the time when navigating code, so it’s nice to have it laid out neatly.