gpt4 book ai didi

ios - Swift:在协议(protocol)一致性中覆盖协议(protocol)扩展的关联类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:43 25 4
gpt4 key购买 nike

所以,我正在努力实现这一目标:

有一个带有 associatedtype 的协议(protocol),该协议(protocol)将处理 json 解析到他的扩展中。关联类型必须符合Decodable:

protocol MyProtocol {
associatedtype ResponseType: Decodable
func handleResponse(data: Data) -> ResponseType
}

我想要做的是将 responseType 的默认类型设置到我的扩展中,然后,如果需要,将该类型覆盖到类或结构一致性中。像这样。

extension MyProtocol {
typealias ResponseType = MyDefaultDecodableType

func handleResponse(data: Data) -> ResponseType { ... }
}

class MyObject: MyProtocol {
typealias ResponseType = AnotherDecodableType
}

问题是我在 MyObject 中遇到这样的错误:

error: type 'MyObject' does not conform to protocol 'MyProtocol'
class MyObject: MyProtocol {
^
note: multiple matching types named 'ResponseType'
associatedtype ResponseType: Decodable
^
note: possibly intended match
typealias ResponseType = AnotherDecodableType
^
note: possibly intended match
public typealias ResponseType = MyDefaultDecodableType

我不知道是否有可能实现我正在尝试的目标,或者我正在接近错误的方式。谁能给我一些启发?

谢谢。

最佳答案

我已经创建了相同的代码。这里有一些事实需要了解。

extension MyProtocol {
typealias ResponseType = MyDefaultDecodableType

func handleResponse(data: Data) -> ResponseType {

return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)

}
}

从概念上讲,Swift 中没有泛型协议(protocol)。但是通过使用 typealias,我们可以为另一种类型声明一个必需的别名。

您的扩展不需要定义 typealias ResponseType = MyDefaultDecodableType,因为它将使用 MyDefaultDecodableType 提供一些默认实现,因此它没有用。

所以你的扩展应该是这样的

extension MyProtocol {
// typealias ResponseType = MyDefaultDecodableType // NO NEED FOR IT

func handleResponse(data: Data) -> MyDefaultDecodableType {
print("Test \(self)")
return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)

}
}

现在你可以定义

class MyObject:MyProtocol {
typealias ResponseType = AnotherDecodableType

func handleResponse(data: Data) -> ResponseType {
print("Test \(self)")

return try! JSONDecoder().decode(AnotherDecodableType.self, from: data)

}

}
class MyObject2:MyProtocol {


}

没有任何错误

现在如果你使用

MyObject().handleResponse(data:data)
MyObject2().handleResponse(data:data2)

你会得到

test __lldb_expr_44.MyObject

test __lldb_expr_44.MyObject2

关于ios - Swift:在协议(protocol)一致性中覆盖协议(protocol)扩展的关联类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48791429/

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