#pragma mark in Swift?

i0S Swift Issue

Question or problem with Swift language programming:

In Objective C, I can use #pragma mark to mark sections of my code in the symbol navigator. Since this is a C preprocessor command, it’s not available in Swift. Is there a stand-in for this in Swift, or do I have to use ugly comments?

How to solve the problem:

Solution 1:

You can use // MARK:


There has also been discussion that liberal use of class extensions might be a better practice anyway. Since extensions can implement protocols, you can e.g. put all of your table view delegate methods in an extension and group your code at a more semantic level than #pragma mark is capable of.

Solution 2:

Up to Xcode 5 the preprocessor directive #pragma mark existed.

From Xcode 6 on, you have to use // MARK:

These preprocessor features allow to bring some structure to the function drop down box of the source code editor.

some examples :

// MARK:

-> will be preceded by a horizontal divider

// MARK: your text goes here

-> puts ‘your text goes here’ in bold in the drop down list

// MARK: - your text goes here

-> puts ‘your text goes here’ in bold in the drop down list, preceded by a horizontal divider

update : added screenshot ’cause some people still seem to have issues with this :

enter image description here

Solution 3:

For those who are interested in using extensions vs pragma marks (as mentioned in the first comment), here is how to implement it from a Swift Engineer:

import UIKit

class SwiftTableViewController: UITableViewController {

    init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

extension SwiftTableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
        let cell = tableView?.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell;

        cell.textLabel.text = "Hello World"

        return cell
    }

}

It’s also not necessarily the best practice, but this is how you do it if you like.

Solution 4:

Pragma mark - [SOME TEXT HERE] was used in Objective-C to group several function together by line separating.

In Swift you can achieve this using MARK, TODO OR FIXME

i. MARK : //MARK: viewDidLoad

This will create a horizontal line with functions grouped under viewDidLoad(shown in screenshot 1)

Screenshot 1

ii. TODO : //TODO: - viewDidLoad

This will group function under TODO: – viewDidLoad category (shown in screenshot 2)

Screenshot 2

iii. FIXME : //FIXME - viewDidLoad

This will group function under FIXME: – viewDidLoad category (shown in screenshot 3)

Screenshot 3

Check this apple documentation for details.

Solution 5:

Official Documentation

Apple’s official document about Xcode Jump Bar: Add code annotations to the jump bar

Jump Bar Screenshots for Sample Code

Behavior in Xcode 10.1 and macOS 10.14.3 (Mojave)

Behavior in Xcode 10.0 and macOS 10.13.4 (High Sierra)

Behavior in Xcode 9.4.1 and macOS 10.13.0

Discussion

!!!: and ???: sometimes are not able to be displayed.

Hope this helps!