Categories
iOS ObjectiveC Xcode

HowTo: Organize ObjectiveC code with the pragma mark directive

Welcome to Part 9 of my blog series about game development: pragma mark directive

 

Todays post is very short. I’ll only show how the #pragma mark directive can help you to organize your code. The navigation bar on top of the code window provides the possibility to navigate in your code. Just click on the last entry:

 
This will open a drop down list of all available constants, properties and methods. By selecting one element in the list, the code windows scrolls to the corresponding code section. So far so good, but  the methods are listed in alphabetical order. This becomes quite messy if the code for your class is growing.
 

A better way to organize your code would be grouping elements by their context. For example all methods which belongs to the HUD should be grouped together. Luckily Objective C provides a solution for that: The #pragma mark‘ directive.
 
Just enter some #pragma marks to your code:
 
 
#pragma mark – Init
 
….
 
#pragma mark – HUD handling
 
You can see the result, if your click again in the navigation bar to open the drop down list:
 
 
 
 

That’s all for today. In my next post I’ll show how to use the delegate pattern to communicate from an SKScene with the parent view controller class.

Cheers,
Stefan