0Day Forums
#pragma mark in Swift? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: #pragma mark in Swift? (/Thread-pragma-mark-in-Swift)

Pages: 1 2 3


#pragma mark in Swift? - Sirnorineztp - 07-18-2023

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?


RE: #pragma mark in Swift? - chews748234 - 07-18-2023

Apple states in the latest version of [*Building Cocoa Apps*][1],

> The Swift compiler does not include a preprocessor. Instead, it takes
> advantage of compile-time attributes, build configurations, and
> language features to accomplish the same functionality. For this
> reason, preprocessor directives are not imported in Swift.

The # character appears to still be how you work with various build configurations and things like that, but it looks like they're trying to cut back on your need for most preprocessing in the vein of pragma and forward you to other language features altogether. Perhaps this is to aid in the operation of the Playgrounds and the REPL behaving as close as possible to the fully compiled code.

[1]:


RE: #pragma mark in Swift? - Drcurvy7 - 07-18-2023

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.



RE: #pragma mark in Swift? - rayshellsjgucyx - 07-18-2023

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


RE: #pragma mark in Swift? - ganch794566 - 07-18-2023

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.


RE: #pragma mark in Swift? - douglassxuzml - 07-18-2023

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][1]


[1]:



RE: #pragma mark in Swift? - disaffiliated347010 - 07-18-2023

`//MARK:` does not ***seem*** to work for me in Xcode 6.3.2. However, this is what I did to ***get it to work***:

1) Code:

import Cocoa

class MainWindowController: NSWindowController {

//MARK: - My cool methods

func fly() {
}

func turnInvisible() {

}
}

2) In the `jump bar` nothing appears to change when adding the `//MARK`: comment. However, if I click on the rightmost name in the jump bar, in my case it says `MainWindowController(with a leading C icon)`, then a popup window will display showing the effects of the //MARK: comment, namely a heading that says "My cool methods":

![enter image description here][1]

3) I also notice that if I click on one of the methods in my code, then the method becomes the rightmost entry in the jump bar. In order to get `MainWindowController(with a leading C icon)` to be the rightmost entry in the jump bar, I have to click on the whitespace above my methods.


[1]:



RE: #pragma mark in Swift? - warlikeness843250 - 07-18-2023

//# 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][1]][1]


[1]:



RE: #pragma mark in Swift? - ozokerites964123 - 07-18-2023

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

[![enter image description here][1]][1]


[1]:



RE: #pragma mark in Swift? - extraconstitutional445413 - 07-18-2023

I think [`Extensions`][1] 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) {
...
}
}


[1]:

[To see links please register here]