我注意到 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 declaredstatic
:protocol Equatable { static func ==(lhs: Self, rhs: Self) -> Bool }
关于swift - Swift 3 协议(protocol)中的静态关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944654/