问题是当一个结构符合协议(让我们称它为PA)和可解码时,但是PA强加了一个类型为不可解码的属性。示例:
protocol PA {
var b: [PB]? { get }
}
protocol PB {}
struct SA: PA, Decodable {
let b: [PB]? // SA's conformance to Decodable wants this to be [Decodable], but PA's conformance imposes [PB]
}
struct SB: PB, Decodable {}上述代码拒绝编译,因为:
'Decodable'
将这一行改为:
let b: [PB & Decodable]?也不起作用,并给出:
'Decodable'
请注意,第4行是非意义的:“‘可解码& PB?’不符合‘可解码’”。等等什么?
有什么建议吗?
发布于 2020-08-05 11:19:59
您可以创建一个混合协议:
protocol PADecodable {
var b: [PB & Decodable]? { get }
}
struct SA: PADecodable {
let b: [PB & Decodable]?
}发布于 2020-08-05 09:26:21
你可以通过以下方法来修复它:
protocol PA {
var b: [PB]? { get }
}
protocol PB {}
struct SA<T: PB & Codable>: PA, Codable {
private var _b: [T]?
var b: [PB]? {
return _b
}
}https://stackoverflow.com/questions/63261881
复制相似问题