在Objective C中,我可以使用#pragma mark来标记符号导航器中的代码片段。由于这是一个C预处理器命令,所以在Swift中不可用。在Swift中有替代品吗,或者我必须使用丑陋的评论吗?
当前回答
我认为Extensions是一个更好的方式而不是#pragma mark。
使用扩展前的代码:
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) {
...
}
}
使用扩展后的代码:
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) {
...
}
}
其他回答
在Xcode 5之前,预处理器指令#pragma标记存在。
从Xcode 6开始,你必须使用// MARK:
这些预处理器特性允许为源代码编辑器的函数下拉框带来一些结构。
一些例子:
// MARK:
->前面有一个水平分隔符
// MARK: your text goes here
->在下拉列表中将“您的文本放在这里”以粗体显示
// MARK: - your text goes here
->在下拉列表中将“您的文本放在这里”以粗体显示,前面有一个水平分隔符
更新:增加了截图,因为有些人似乎仍然有问题:
Add a to-do item: Insert a comment with the prefix TODO:. For example: // TODO: [your to-do item]. Add a bug fix reminder: Insert a comment with the prefix FIXME:. For example: // FIXME: [your bug fix reminder]. Add a heading: Insert a comment with the prefix MARK:. For example: // MARK: [your section heading]. Add a separator line: To add a separator above an annotation, add a hyphen (-) before the comment portion of the annotation. For example: // MARK: - [your content]. To add a separator below an annotation, add a hyphen (-) after the comment portion of the annotation. For example: // MARK: [your content] -.
对于那些对使用扩展和pragma标记感兴趣的人(如第一条评论所述),下面是如何从Swift工程师实现它:
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
}
}
这也不一定是最佳实践,但如果您愿意,可以这样做。
Pragma标记是一种提高代码可读性的方法。pragma注释会像Xcode跳跃栏上的标签一样出现。
//MARK: <Your comment goes here>
示例:在代码中,
//MARK: Properties
// MARK: View Life cycle
//MARK: Helper methods
这是它在Xcode跳转栏中的显示方式。
你可能也会对Swift 4.2 / XCode 10编译器指令感兴趣
#warning("Some string to display")
and
#error("Some error to display")
当你真的不想错过什么东西时,它可能会很有用。