gpt4 book ai didi

swift - 如何消除静态方法和实现协议(protocol)之间的歧义?

转载 作者:行者123 更新时间:2023-11-30 10:10:58 25 4
gpt4 key购买 nike

对于实例函数来说,转换效果很好。

protocol Protocol1 {}
extension Protocol1 {
func instanceFunc() {}
static func staticFunc() {}
}

protocol Protocol2 {}
extension Protocol2 {
func instanceFunc() {}
static func staticFunc() {}
}

struct Struct: Protocol1, Protocol2 {
func instanceFunc() {
(self as Protocol1).instanceFunc()
(self as Protocol2).instanceFunc()
}

static func staticFunc() {
// My best guess. Nope!
(Struct as Protocol1.Type).staticFunc()
(Struct as Protocol2.Type).staticFunc()
}
}

最佳答案

这应该可以解决问题,伙计

protocol Protocol1 {}
extension Protocol1 {
func instanceFunc() {}
static func staticFunc() {}
}

protocol Protocol2 {}
extension Protocol2 {
func instanceFunc() {}
static func staticFunc() {}
}

struct Struct: Protocol1, Protocol2 {
func instanceFunc() {
(self as Protocol1).instanceFunc()
(self as Protocol2).instanceFunc()
}

static func staticFunc() {
(Struct.self as Protocol1.Type).staticFunc()
(Struct.self as Protocol2.Type).staticFunc()
}
}

关于swift - 如何消除静态方法和实现协议(protocol)之间的歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33087267/

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