gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:05:24 28 4
gpt4 key购买 nike

我注意到 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com