为什么不符合协议可编码的要求?

时间:2019-10-13 08:46:45

标签: swift encodable

我具有以下代码结构。如果我省略了codingkey部分,则代码正在运行。我实现了StringConverter来将字符串转换为Int(来自bySundell Swift Side)

struct Video: Codable {
    var title: String
    var description: String
    var url: URL
    var thumbnailImageURL: URL

    var numberOfLikes: Int {
        get { return likes.value }

    }

    private var likes: StringBacked<Int>
    enum CodingKeys: String, CodingKey{
        case title = "xxx"
        case description = "jjjj"
        case url = "url"
        case thumbnailImageURL = "jjjjjjjj"
        case numberofLikes = "jjjjjkkkk"

    }

}


// here the code for converting the likes
protocol StringRepresentable: CustomStringConvertible {
    init?(_ string: String)
}

extension Int: StringRepresentable {}
struct StringBacked<Value: StringRepresentable>: Codable {
    var value: Value

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)
        let stringToConvert = string.split(separator: "/").last!.description
        guard let value = Value(stringToConvert) else {
            throw DecodingError.dataCorruptedError(
                in: container,
                debugDescription: """
                Failed to convert an instance of \(Value.self) from "\(string)"
                """
            )
        }

        self.value = value
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(value.description)
    }}

正如我所说,如果我省略了Codingkeys部分,它不会显示任何错误。我将简单地创建一个结构,从Rest API中获取一个字符串,并希望使用Codable在数据库中转换为Int。 谢谢 阿诺德

1 个答案:

答案 0 :(得分:1)

定义CodingKeys时,需要为每个非可选/未初始化的属性提供密钥,以便编译器知道如何在解码时进行初始化。将此应用到Video上,它将看起来像这样,

struct Video: Codable {

    var title: String
    var description: String
    var url: URL
    var thumbnailImageURL: URL

    var numberOfLikes: Int {
        return likes.value

    }

    private var likes: StringBacked<Int>

    enum CodingKeys: String, CodingKey{
        case title = "xxx"
        case description = "jjjj"
        case url = "url"
        case thumbnailImageURL = "jjjjjjjj"
        case likes = "jjjjjkkkk"

    }
}

如果仔细观察,枚举中未提供此属性private var likes: StringBacked<Int>,则CodingKey会因此引起编译器的抱怨。在这种情况下,我用case likes = "jjjjjkkkk"更新了枚举,并删除了case numberofLikes = "jjjjjkkkk",因为numberofLikes是一个只读的计算属性,不需要任何解析。