如何使用 `Codable` 协议调用 `didSet` 方法

How can I call `didSet` method using `Codable` protocol

如何使用 Codable 协议调用 didSet 方法。

    class Sample: Codable{
        var text : String? {
            didSet {
                print("didSet") // do not call
                extended_text = "***" + text! + "***"
            }
        }
        var extended_text : String?
    }

    let sample_json = "{\"text\":\"sample text\"}"
    let decoder = JSONDecoder()
    let sample = try! decoder.decode(Sample.self, from: sample_json.data(using: .utf8)!)
    print(sample.text!)
    print(sample.extended_text ?? "") 

而不是使用 didSet,你应该将 extendedText 设为只读计算 属性。请注意,在命名您的属性时,使用 camelCase 而不是 snake_case 是 Swift 惯例:

struct Sample: Codable {
    let text: String
    var extendedText: String {
        return "***" + text + "***"
    }
}

let sampleJson = """
{"text":"sample text"}
"""

do {
    let sample = try JSONDecoder().decode(Sample.self, from: Data(sampleJson.utf8))
    print(sample.text)            // "sample text\n"
    print(sample.extendedText)    // "***sample text***\n"
} catch {
    print(error)
}

如果您的目标是 运行 初始化 Codable 结构时的一种方法是编写您自己的自定义解码器:

class Sample: Codable {
    let text: String
    required init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        text = try container.decode(String.self)
        print("did set")
    }
}

let sampleJson = "{\"text\":\"sample text\"}"
let decoder = JSONDecoder()
do {
    let sample = try decoder.decode([String: Sample].self, from: Data(sampleJson.utf8))
    print(sample["text"]?.text ?? "")
} catch {
    print(error)
}

这将打印:

did set

sample text