Swift中的#pragma mark?

本文翻译自:#pragma mark in Swift?

In Objective C, I can use #pragma mark to mark sections of my code in the symbol navigator. 在Objective C中,我可以使用#pragma mark在符号导航器中标记我的代码部分。 Since this is a C preprocessor command, it's not available in Swift. 由于这是一个C预处理器命令,因此在Swift中不可用。 Is there a stand-in for this in Swift, or do I have to use ugly comments? 在Swift中是否有替代,或者我是否必须使用丑陋的评论?


#1楼

参考:https://stackoom.com/question/1cm04/Swift中的-pragma-mark


#2楼

In Objective-C code Xcode detects comments like // MARK: - foo which is a bit more portable than #pragma . 在Objective-C代码中,Xcode检测像// MARK: - foo这样的评论,它比#pragma更容易携带。 But these do not seem to be picked up, too (yet?). 但这些似乎也没有被提升(但是?)。

Edit: Fixed in Xcode 6 beta 4. 编辑:已在Xcode 6 beta 4中修复。


#3楼

Apple states in the latest version of Building Cocoa Apps , Apple在最新版本的Building Cocoa Apps中表示

The Swift compiler does not include a preprocessor. Swift编译器不包含预处理器。 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. 因此,预处理程序指令不会在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. #字符似乎仍然是你使用各种构建配置和类似的东西的方式,但看起来他们试图在pragma的静脉中减少对大多数预处理的需求,并将你转发到其他语言功能。 Perhaps this is to aid in the operation of the Playgrounds and the REPL behaving as close as possible to the fully compiled code. 也许这是为了帮助Playgrounds和REPL的操作尽可能接近完全编译的代码。


#4楼

You can use // MARK: 你可以使用// 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 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. 由于扩展可以实现协议,因此您可以将所有表视图委托方法放在扩展中,并将代码分组到比#pragma mark能够更加语义的级别。


#5楼

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: 对于那些对使用扩展与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
    }

}

It's also not necessarily the best practice, but this is how you do it if you like. 它也不一定是最好的做法,但如果你愿意,这就是你如何做到的。


#6楼

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. 今天早上在WWDC上与Swift实验室的Apple工程师确认目前没有任何#pragma或同等版本,他们认为这是一个错误,它很快就会到来,所以我猜测测试2,我希望。

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 Xcode现在支持// MARK:,// TODO:和// FIXME地标来注释你的代码并在跳转栏中列出它们

发布了0 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/p15097962069/article/details/105340088
今日推荐