2

我们刚刚切换到 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 中一样工作。

4

1 回答 1

1

我对 BinaryCoder 有类似的问题并创建了解决方法。但是您必须有权访问编码器和解码器实现。

protocol XDRCodableArray {
    func binaryEncode(to encoder: XDREncoder) throws
    init(fromBinary decoder: XDRDecoder) throws
}

extension Array: XDRCodableArray {
   //copy implementation of XDRCodable from Array: XDRCodable
}

//delete extension Array: XDRCodable

在 decode append 数组的特殊实现中:

...
case let binaryT as BinaryDecodable.Type:
    return try binaryT.init(fromBinary: self) as! T
//new case
case let array as BinaryCodableArray.Type:
    return try array.init(fromBinary: self) as! T
...     

而且在编码:

...
case let binary as BinaryEncodable:
    try binary.binaryEncode(to: self)
//new case    
case let array as BinaryCodableArray:
    try array.binaryEncode(to: self)
...

一般来说,条件协议一致性存在问题。您不能将变量转换为该协议(在某些情况下)。

protocol Test: Codable {
    func test() -> String
}

extension Array: Test where Element: Codable {
    func test() -> String {
        return "Success"
    }
}

func doSomething(x: Codable) {
   let test = x as! Test
   test.test()
}


let array = ["value"]
let t = (array as! Test).test   //success
doSomething(x: array)   //fail on first row inside function

我希望具有动态查询条件一致性支持的 Swift 4.2 能够解决这个问题。

于 2018-04-03T13:30:30.080 回答