我们刚刚切换到 swift 4.1,我们在数组的类型一致性方面遇到了一些困难。这是旧方法:
public typealias XDRCodable = XDREncodable & XDRDecodable
public protocol XDREncodable: Encodable {
func xdrEncode(to encoder: XDREncoder) throws
}
public protocol XDRDecodable: Decodable {
init(fromBinary decoder: XDRDecoder) throws
init(fromBinary decoder: XDRDecoder, count: Int) throws
}
extension Array: XDRCodable {
public func xdrEncode(to encoder: XDREncoder) throws {
try encoder.encode(UInt32(self.count))
for element in self {
try (element as! Encodable).encode(to: encoder)
}
}
public init(fromBinary decoder: XDRDecoder) throws {
guard let binaryElement = Element.self as? Decodable.Type else {
throw XDRDecoder.Error.typeNotConformingToDecodable(Element.self)
}
let count = try decoder.decode(UInt32.self)
self.init()
self.reserveCapacity(Int(count))
for _ in 0 ..< count {
let decoded = try binaryElement.init(from: decoder)
self.append(decoded as! Element)
}
}
}
这在 swift 4.1 中会出现以下错误:
'XDRDecodable' 要求 'Element' 符合 'Decodable'
所以我们尝试将声明更改为:
extension Array: XDRCodable where Element : XDRCodable
虽然编译它仍然无法对数组进行编码,并生成以下警告:
警告:Swift 运行时还不支持动态查询条件一致性('Swift.Array': 'stellarsdk.XDREncodable')
我看到这是一项正在进行的工作,但是在正确实现类型一致性之前,是否有人对此有解决方法。我希望它能够像现在在 swift 4.0 中一样工作。