Swift

Swift之Codable协议的实现

如何给带有@Published属性的calss遵循Codable协议

class Order: Codable {
    @Published var num = 1
    @Published var isAddIce = false
    @Published var isAddSugar = false
    
    @Published var name = ""
    @Published var phone = ""
    @Published var address = ""
}

报错

Type 'Order' does not conform to protocol 'Decodable'
Type 'Order' does not conform to protocol 'Encodable'

实现Codable协议

// 1. 规定哪些属性需要被编码和解码
    enum CodingKeys: CodingKey {
        case num, isAddIce, isAddSugar, name, phone, address
    }
    
    // 2. 自定义解码功能
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        num = try container.decode(Int.self, forKey: .num)
        isAddIce = try container.decode(Bool.self, forKey: .isAddIce)
        isAddSugar = try container.decode(Bool.self, forKey: .isAddSugar)
        name = try container.decode(String.self, forKey: .name)
        phone = try container.decode(String.self, forKey: .phone)
        address = try container.decode(String.self, forKey: .address)
    }
    
    // 3. 自定义编码
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        
        try container.encode(num, forKey: .num)
        try container.encode(isAddIce, forKey: .isAddIce)
        try container.encode(isAddSugar, forKey: .isAddSugar)
        try container.encode(name, forKey: .name)
        try container.encode(phone, forKey: .phone)
        try container.encode(address, forKey: .address)
    }