#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?
You can use // MARK:
Historical, Prior to Xcode 6 Beta 4
Just talked to an Engineer here at WWDC, and the current beta of Xcode doesn't implement the
// MARK:
style yet, but I'm told future versions will.
It was also suggested that making liberal use of class extensions might be a better practice anyway. Since extensions can implement protocols, you can eg 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.
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.
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 :
链接地址: http://www.djcxy.com/p/56488.html上一篇: 将Int转换为Swift中的字符串
下一篇: Swift中的#pragma标记?