简体   繁体   English

类不符合 Encodable

[英]Class does not conform to Encodable

However, getting an error message that says that my class 'Expenses' does not conform to protocol 'Decodable' & Type 'Expenses' does not conform to protocol 'Encodable'但是,收到一条错误消息,指出我的类“费用”不符合协议“可解码”和“费用”类型不符合协议“可编码”

import Foundation

class Expenses : ObservableObject, Codable  {
    
    @Published var items : [ExpenseItem] {
        // Step 1 creat did set on publsihed var.
        didSet {
            let encoder = JSONEncoder()
            if let encoded = try? encoder.encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }

    init() {
        if let items = UserDefaults.standard.data(forKey: "Items") {
            let decoder = JSONDecoder(
            if let decoded = try?
                decoder.decode([ExpenseItem].self, from: items) {
                self.items = decoded
                return
            }
        }
        self.items = []
    }     
}

my expense item is flagged as我的费用项目被标记为

struct ExpenseItem : Identifiable, Codable   {
    let id = UUID()
    let name : String
    let type : String
    let amount : Int       
}

Conformance to Encodable / Decodable is auto-synthesized when all stored properties conform to Encodable / Decodable , but using a property wrapper on a property means that now the property wrapper type needs to conform to Encodable / Decodable .一致性Encodable / Decodable是自动合成时所有存储的性能符合Encodable / Decodable ,但使用上的属性的装置,现在的属性包装类型的需求,以符合一属性包装Encodable / Decodable

@Published property wrapper doesn't conform. @Published属性包装器不符合。 It would have been nice to just implement conformance on the Published type itself, but unfortunately it doesn't expose the wrapped value, so without using reflection (I've seen suggestions online), I don't think it's possible.只在Published类型本身上实现一致性会很好,但不幸的是它没有公开包装的值,所以不使用反射(我在网上看到了建议),我认为这是不可能的。

You'd need to implement the conformance manually:您需要手动实现一致性:

class Expenses : ObservableObject {
   @Published var items : [ExpenseItem]

   // ... rest of your code
}

extension Expense: Codable {
   enum CodingKeys: CodingKey {
      case items
   }
    
   func encode(to encoder: Encoder) throws {
      var container = encoder.container(keyedBy: CodingKeys.self)
      try container.encode(self.items, forKey: .items)
   }

   required init(from decoder: Decoder) throws {
      var container = try decoder.container(keyedBy: CodingKeys.self)
      self.items = try container.decode([ExpenseItem].self, forKey: .items)
   }      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“PlayerData”不符合协议“Decodable”和“Encodable” - Type 'PlayerData' does not conform to protocol 'Decodable' and 'Encodable' 类型“模型”不符合协议“可解码”/可编码 - Type 'Model' does not conform to protocol 'Decodable'/Encodable 类型 '' 不符合协议 'Decodable'/'Encodable' - Type '' does not conform to protocol 'Decodable'/'Encodable' 类型 'Favorites.Type' 不能符合 'Encodable'; 只有结构/枚举/类类型可以符合协议 - Type 'Favorites.Type' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols Swift Decodable Class 不符合 - Swift Decodable Class does not conform 访问 @Published 变量不会编译; “不能符合‘视图’; 只有结构/枚举/类类型可以符合协议” - Access a @Published variable does not compile; “cannot conform to 'View'; only struct/enum/class types can conform to protocols” SwiftUI-类型“服务”不符合协议“可解码” - SwiftUI - Type 'Service' does not conform to protocol 'Decodable' 是的'MapView'不符合协议'UIViewRepresentable' - ype 'MapView' does not conform to protocol 'UIViewRepresentable' 类型“SwiftUIWebView”不符合协议“NSViewRepresentable” - Type 'SwiftUIWebView' does not conform to protocol'NSViewRepresentable 参数类型不符合预期类型“WKScriptMessageHandler” - Argument Type does not conform to expected type 'WKScriptMessageHandler'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM