如何在可编码结构中使用计算属性(swift)

时间:2018-03-15 21:49:20

标签: ios json swift codable computed-properties

我创造了一个可编码的" struct序列化数据集并将其编码为Json。除了计算出的属性不会显示在json字符串中之外,一切都工作得很好。如何在编码阶段包含计算属性。

例如:

struct SolidObject:Codable{

    var height:Double                      = 0
    var width:Double                       = 0
    var length:Double                      = 0

    var volume:Double {
        get{
            return height * width * length
        }
    }
}

var solidObject = SolidObject()
solidObject.height = 10.2
solidObject.width = 7.3
solidObject.length = 5.0

let jsonEncoder = JSONEncoder()
do {
    let jsonData = try jsonEncoder.encode(solidObject)
    let jsonString = String(data: jsonData, encoding: .utf8)!
    print(jsonString)
} catch {
    print(error)
}

打印出" {"宽度":7.2999999999999998,"长度":5,"高度":10.199999999999999}"

我也很好奇有7.29999而不是7.3但我的主要问题是"我怎么能包括"音量"这个json字符串也是#34;?

1 个答案:

答案 0 :(得分:10)

您需要手动编码/解码,而不是让自动化的东西为您完成。这在Swift游乐场中按预期工作。

struct SolidObject: Codable {

    var height:Double                      = 0
    var width:Double                       = 0
    var length:Double                      = 0

    var volume:Double {
        get{
            return height * width * length
        }
    }

    enum CodingKeys: String, CodingKey {
        case height
        case width
        case length

        case volume
    }

    init() { }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        height = try values.decode(Double.self, forKey: .height)
        width = try values.decode(Double.self, forKey: .width)
        length = try values.decode(Double.self, forKey: .length)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(height, forKey: .height)
        try container.encode(width, forKey: .width)
        try container.encode(length, forKey: .length)

        try container.encode(volume, forKey: .volume)
    }

}

var solidObject = SolidObject()
solidObject.height = 10.2
solidObject.width = 7.3
solidObject.length = 5.0

let jsonEncoder = JSONEncoder()
do {
    let jsonData = try jsonEncoder.encode(solidObject)
    let jsonString = String(data: jsonData, encoding: .utf8)!
    print(jsonString)
} catch {
    print(error)
}