swift - Swift 3 协议(protocol)中的静态关键字

标签 swift static swift3 swift-protocols

我注意到 Swift protocols 中的某些函数具有 static 关键字。但是,当您实现该功能时,您必须删除 static 关键字以使编译器满意。

public static func <(lhs: Self, rhs: Self) -> Bool

static 在上下文中是什么意思,它的目的是什么?

最佳答案

来自 Xcode 8 beta 4 发行说明:

Operators can be defined within types or extensions thereof. For example:

struct Foo: Equatable {
    let value: Int
    static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return lhs.value == rhs.value
    }
}

Such operators must be declared as static (or, within a class, class final), and have the same signature as their global counterparts. As part of this change, operator requirements declared in protocols must also be explicitly declared static:

protocol Equatable {
    static func ==(lhs: Self, rhs: Self) -> Bool
}

关于swift - Swift 3 协议(protocol)中的静态关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944654/

相关文章:

ios - 是否可以在 UIImagePickerController 中以编程方式调用 didFinishPickingMediaWithInfo

swift - Swift 中的多个 For 循环条件

ios - 快速缓存.. iOS FBApi

ios - 共享扩展到即将到来的主机应用程序断点在 swift 中不起作用

ios - 确保输入的 Double 具有特定的格式

java - 对家长 Activity 使用静态有什么问题?

swift - 使用 http ://对 URL 进行编码

java - 静态 block 内的嵌套类未被外部类的方法识别

java - 是否有等同于@Override 的用于覆盖(隐藏)静态方法的注解?

ios - 在 Swift/XCode 8 中从 subview 更改父 View ?