top of page

#pragma mark and custom warnings and error in XCode

Updated: May 9, 2024


A tree with a marking on it

Let’s make your iOS Project easier to read and navigate through!

The goal of this article is not writing code that compiles faster or writing shorter code using higher order functions but more of a good programming practice. So repeat after me:


Clean code meme

We spend hours and hours coding in front of our screen and spend days or months or even longer to create a product that we can finally show to our client. Sometimes , we are in such a hurry for completing our code that we essentialy forget putting out comments . And then, after say a long vacation (for the successful launch of the product), you come back to work on your codebase and you just sit in front of that screen, wondering, why in the world did I wrote that logic:


Thinking deeply

Face it, if you’re not using comments in your code, you have probably faced the same situation like this one.

But, the point of this article are not comments, but rather, #pragma mark. #pragma mark is an Objective-C terminology and there is no definite name for it in Swift programming language. So i will call them, pragmas (as there are more than just one #pragma mark).

Let’s write a code for a simple tableView with some of its delegate and dataSource methods to best show pragmas use-case.


Simple tableView implementation

And if you click on the No Selection (maybe showing a method in your case ) on the jump bar at top :


No selection marker

You’ll see this :


Tapping on no selection marker

All the methods and variables are present there as a collection without any separation between them, which, to be honest, look ugly.

The code will work fine, but, to be honest, without all the comments and no separation between what are data source methods and what are delegate’s, it looks a bit messy. So let's clean up the code a little by adding comments and separating the UITableView 's dataSource and delegate methods by introducing extension.


Refactoring using extensions
Refactoring using extensions

This cleans up our code a bit and provides separation between data source and delegate methods. /// on top of our tableView object is not actually a comment but rather a documentation markup in Xcode, which are a bit different from comments. I would not explain that here, as it will just add lengths to the article.

Now coming back to the point, we’re halfway through. If we again click on that jump bar , we will now see extensions to the view controller added to it with the methods separated, but it still doesn’t tell what’s what.


Method and variables are now split

Now here’s where our pragmas come in handy.

Pragmas are , you can say, special type of comments that start with a particular keyword , and then let us define/write what we want.

They always start with // and not /// as /// signifies documentation markup and // signifies comments. This // is followed by MARK keyword as is, all capitalised and followed by a : with no space in between . So your final keyword will look like this — // MARK: . Even if you add this , you wont get much in the jump bar, just an icon with a blank space after it.

The magic lies in what you write after this . Whatever you write after this keyword will be added exactly like that in the jump bar. But first, if you want some separation , say like a horizontal line, between different pragmas, you need to add - . Now it will look like // MARK: - . Now, everything you write after this will be added as such inside the jump bar . Let’s have some fun, shall we :


Adding pragma mark

I created // MARK: — Custom UI Elements and the same text is added to the jump bar. Now let’s complete our View Controller and fill it with pragmas.


Adding more pragma marks
Adding more pragma marks

You’ll have to admit that with just a few modifications , our code looks cleaner. And above all , our jump bar looks like this —


Navigator after pragma marks

Here’s a quick little comparison of the two approach —


Navigator without and with pragma marks
Left — Without pragmas || Right — With pragmas

Other than MARK we have a bonus of TODO: and FIXME: as well , which have different icons in the jump bar as well and which follows the same pattern as the pragma we defined above : // MARK: -

Other than pragmas, if we wanted to define our custom made warnings and error in Xcode , we can make use of #error(“”) and #warning(“”) as follows:


Custom warning and errors

Site's author picture

Shubham Bakshi

Been coding since '13. Rode the Apple bandwagon in '18. Some can say it was love at first byte. I engineer apps with such finesse, they practically dance through your device. And if you're a dev feeling lost in the code jungle, fear not! I'm here to be your coding compass, guiding you through the wilderness of curly braces and semicolons.

Website logo
© Bits & Bytes w/ Bakshi. All rights reserved.
bottom of page