Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#pragma mark in Swift?

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?

like image 464
Arbitur Avatar asked Jun 03 '14 14:06

Arbitur


15 Answers

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.

like image 103
Frank Schmitt Avatar answered Oct 07 '22 19:10

Frank Schmitt


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

like image 29
Ronny Webers Avatar answered Oct 07 '22 21:10

Ronny Webers


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.

like image 41
NatashaTheRobot Avatar answered Oct 07 '22 19:10

NatashaTheRobot


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.

like image 42
Jayprakash Dubey Avatar answered Oct 07 '22 20:10

Jayprakash Dubey


Xcode Official Doc

Apple's official document about Xcode Jump Bar: Add code annotations to the jump bar and minimap introduces these three:

  • TODO:
  • FIXME:
  • MARK:

There are two more (though not in the doc):

  • !!!:
  • ???:

which are not supported by some Xcode versions (such as v10.0), but are supported by latest version (v13.3.1).

Screenshots for Sample Versions of Xcode

Sample Code

Example 1 - Xcode 10.1 + macOS 10.14.3 (Mojave)

Xcode 10.1 and macOS 10.14.3

Example 2 - Xcode 10.0 + macOS 10.13.4 (High Sierra)

Xcode 10.0 and macOS 10.13.4

like image 27
George Avatar answered Oct 07 '22 20:10

George


In Objective-C code Xcode detects comments like // MARK: - foo which is a bit more portable than #pragma. But these do not seem to be picked up, too (yet?).

Edit: Fixed in Xcode 6 beta 4.

like image 28
Nikolai Ruhe Avatar answered Oct 07 '22 21:10

Nikolai Ruhe


Xcode 8 now handles it as followed and shows up like this in the method dropdown:

enter image description here

like image 20
Antoine Avatar answered Oct 07 '22 20:10

Antoine


I think Extensions is a better way instead of #pragma mark.

The Code before using Extensions:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    ...

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        ...
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        ...
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        ...
    }
}

The code after using Extensions:

class ViewController: UIViewController {
    ...
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        ...
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        ...
    }
}

extension ViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       ...
    }
}
like image 32
jqgsninimo Avatar answered Oct 07 '22 20:10

jqgsninimo


Confirmed with an Apple Engineer in the Swift lab this morning at WWDC that there currently aren't any #pragma or equivalent at the moment, they consider this a bug, and it will arrive soon, so I am guessing beta 2, I hope.

Anyway, it's on it's way.


Xcode now supports //MARK:, //TODO: and //FIXME landmarks to annotate your code and lists them in the jump bar

like image 31
Daniel Avatar answered Oct 07 '22 21:10

Daniel


There are Three options to add #pragma_mark in Swift:

1) // MARK: - your text here -

2) // TODO: - your text here -

3) // FIXME: - your text here -

Note: Uses - for add separators

like image 37
Jaydip Avatar answered Oct 07 '22 20:10

Jaydip


Use

// MARK: SectionName

or

// MARK: - SectionName

This will give a line above pragma mark, making it more readable.

For ease just add

// MARK: - <#label#>

to your code snippets.

Alternate way -

Use it in this way

private typealias SectionName = ViewController
private extension SectionName  {
    // Your methods
}

This will not only add mark(just like pragma mark) but also segregate the code nicely.

like image 41
Nikhil Manapure Avatar answered Oct 07 '22 19:10

Nikhil Manapure


//# MARK: - Spinner Class Methods

Add a line between the colon and your description to insert a separator line. This helps to organize your code even more. The code and screenshot above make use of the MARK comment with a line included.

  1. //# MARK: โ€“ Text Methods (LINE)
  2. //# MARK: Text Methods (NO LINE)

This only works with the MARK comment.

enter image description here

like image 22
aashish tamsya Avatar answered Oct 07 '22 19:10

aashish tamsya


Professional programer must be use this tag for good code. It is also good for team work.

// MARK: example Web Service start here
// TODO: example 1
// FIXME: Please change BASE url before live 

It is easy to find method like this

It is easy to find method like this

like image 32
Harshil Kotecha Avatar answered Oct 07 '22 19:10

Harshil Kotecha


In Xcode 11 they added minimap which can be activated Editor -> Minimap.

Minimap will show each mark text for fast orientation in code. Each mark is written like // MARK: Variables

enter image description here

like image 31
MarekB Avatar answered Oct 07 '22 19:10

MarekB


You may also be interested in Swift 4.2 / XCode 10 compiler directives like

#warning("Some string to display")

and

#error("Some error to display")

It might be useful when you really don't want to miss something.

enter image description here

like image 28
fewlinesofcode Avatar answered Oct 07 '22 20:10

fewlinesofcode