You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加@Published属性导致Booking类不遵循Decodable协议的原因

解决@Published导致Codable协议不兼容的问题

问题原因

当给lecturer属性添加@Published后,该属性的实际类型从Lecturer?变为Published<Lecturer?>。Swift的Codable自动合成机制仅处理类的普通存储属性,而Published类型默认不遵循Codable协议,因此编译器无法自动生成Decodable(及Encodable)的实现代码,直接抛出不兼容错误。

此前代码能正常编译,是因为module和lecturer都是普通存储属性,且Module、Lecturer均遵循Codable,编译器自动完成了Codable的合成工作。


解决方案1:手动实现Codable协议方法

最直接的方式是手动编写解码、编码方法,直接操作@Published属性的.value(包装的实际值):

class Booking: ObservableObject, Codable {
    private (set) var module : Module   
    @Published private (set) var lecturer : Lecturer? = nil

    // 定义编码解码的键值
    enum CodingKeys: String, CodingKey {
        case module, lecturer
    }

    init(withModul module : Module) {
        self.module = module
    }

    // 手动实现解码逻辑
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        module = try container.decode(Module.self, forKey: .module)
        lecturer = try container.decodeIfPresent(Lecturer.self, forKey: .lecturer)
    }

    // 手动实现编码逻辑
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(module, forKey: .module)
        try container.encodeIfPresent(lecturer, forKey: .lecturer)
    }
}

解决方案2:让Published全局遵循Codable(谨慎使用)

如果项目中有多个@Published属性需要兼容Codable,可给Published扩展Codable协议(仅当泛型Value遵循Codable时生效):

import Combine

extension Published: Codable where Value: Codable {
    public init(from decoder: Decoder) throws {
        let decodedValue = try Value(from: decoder)
        self.init(initialValue: decodedValue)
    }

    public func encode(to encoder: Encoder) throws {
        try value.encode(to: encoder)
    }
}

这种方式可让编译器重新自动合成Codable实现,但需注意:

  • 扩展会全局生效,可能影响其他使用Published的场景
  • 确保Value类型本身的Codable实现无问题

内容的提问来源于stack exchange,提问作者LukeSideWalker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:10:04