CodingKeys一致性会产生编译器错误

问题描述 投票:0回答:1

我有一个符合DynamicKey的结构CodingKey ..然后我决定扩展已经存在的KeyedEncodingContainer功能,其功能是编码[String: Any] ..

所以现在我到了Struct Foo的一致性部分,但是我得到了一个编译器错误..

有什么想法为什么编译器说Foo.CodingKeys不符合CodingKeys它从DynamicKey继承有一致性?

不工作代码:

struct DynamicKey: CodingKey, Equatable, ExpressibleByStringLiteral {
    var stringValue: String
    var intValue: Int? { return nil }

    init?(stringValue: String) {
        self.stringValue = stringValue
    }

    init?(intValue: Int) {
        return nil
    }

    //MARK:- Equatable Methods
    public static func == (lhs: DynamicKey, rhs: DynamicKey) -> Bool {
        return lhs.stringValue == rhs.stringValue
    }

    //MARK:- ExpressibleByStringLiteral Methods
    public init(stringLiteral value: String) {
        self.stringValue = value
    }

    public init(unicodeScalarLiteral value: String) {
        self.init(stringLiteral: value)
    }

    public init(extendedGraphemeClusterLiteral value: String) {
        self.init(stringLiteral: value)
    }
}

extension KeyedEncodingContainer where Key: CodingKey /*where Key == DynamicKey*/ {
    mutating func encodeDynamicValues(_ value: [String: Any], forKey key: Key) throws {
        //Other code here..
    }
}

struct Foo: Encodable {
    var arr: [String: Any]

    public func encode(to encoder: Encoder) throws {

        //Compiler Error: Instance method 'container(keyedBy:)' requires that 'Foo.CodingKeys' conform to 'CodingKey'
        //However, Foo.CodingKeys conforms to `CodingKey` because `DynamicKey` implements the protocol..
        var container = encoder.container(keyedBy: CodingKeys.self)



        try container.encodeDynamicValues(arr, forKey: .arr)
    }

    enum CodingKeys: DynamicKey {
        case arr
    }
}

但是,如果我将DynamicKey更改为类然后使用&运算符使枚举符合,则编译器错误消失(没有&,它会给出相同的错误)..为什么?

工作守则:

final class DynamicKey: CodingKey { //I don't need the equatable and expressible when it's a class so ignore that part.. adding it doesn't change anything..
    var stringValue: String
    var intValue: Int? { return nil }

    init?(stringValue: String) {
        self.stringValue = stringValue
    }

    init?(intValue: Int) {
        return nil
    }
}

extension KeyedEncodingContainer where Key: CodingKey /*where Key == DynamicKey*/ {
    mutating func encodeDynamicValues(_ value: Any, forKey key: Key) throws {
        //Other Code Here..
    }
}

struct Foo: Encodable {
    var arr: [String: Any]

    public func encode(to encoder: Encoder) throws {
        //CodingKeys now conforms to `CodingKey` because I made `DynamicKey` a class and used the `&` `CodingKey`
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeDynamicValues(arr, forKey: .arr)
    }

    enum CodingKeys: DynamicKey & CodingKey {
        case arr
    }
}
swift swift4.2
1个回答
0
投票

Your first example

第一个例子中的问题只是你对这一行的理解:

enum CodingKeys: DynamicKey

你说:

有什么想法为什么编译器说Foo.CodingKeys在从具有一致性的DynamicKey继承时不符合CodingKeys?

但是Foo.CodingKeys并没有“继承DynamicKey”。什么都不能从结构“继承”。这里的符号与继承无关。你所做的是一个带有DynamicKey类型原始值的枚举。 (通常你不能这样做 - 原始值类型需要“由字符串,整数或浮点字面值表示”,但你通过制作DynamicKey Equatable和ExpressibleByStringLiteral来解决这个问题。)

它的语法与您说的相同:

enum MyEnum : Int {
    case myCase // zero
}

(你也为你的枚举使用魔法名CodingKeys而感到困惑。但这可能不是直接密切相关的。)


Your second example

然后在你的第二个例子中,DynamicKey是一个类,你会以完全相反的方式感到困惑。在这里,你做了一些完全不同的事情:

enum CodingKeys: DynamicKey & CodingKey {

这里,你的枚举没有任何原始值类型;冒号后声明协议限制的东西。但是你在这里以另一种方式欺骗了自己,因为事实上DynamicKey部分是无关紧要的;如果你只是说,代码也可以编译

enum CodingKeys: CodingKey {

在这个例子中真正发生的一切就是你要求自动合成CodingKey的一致性,你已经得到了它。

© www.soinside.com 2019 - 2024. All rights reserved.