#pragma mark in Swift? #pragma mark in Swift? swift swift

#pragma mark in Swift?


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.


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


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 UIKitclass 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.