【问题标题】:My structure does not conform to protocol 'Decodable' / 'Encodable' if I use protocol type in my structure in swift如果我在我的结构中快速使用协议类型,我的结构不符合协议'Decodable'/'Encodable'
【发布时间】:2021-04-16 06:14:54
【问题描述】:

这里我试图从 json 文件中读取数据,并动态转换它。但是如果我在 struct 中使用原型,它会显示does not conform to protocol 'Decodable' / 'Encodable' 错误。如果我在这里遗漏了什么,请告诉我。

struct ScreenData: Codable {
    var id: String
    var objectid : String
    var config : UIConfig
}

protocol UIConfig: class, Codable{
    var bgColor : String? { get set }
}

class LabelConfig : UIConfig {
    var bgColor: String?
    var label : String? = ""
}

class ButtonConfig : UIConfig {
    var bgColor: String?
    var btn_label : String = ""
    //var btn_text_color : UIColor = .black
}

这里我从 json 文件中读取数据并根据数据在堆栈视图中添加组件

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        create stack view to add components
        let stackView = UIStackView()
        stackView.axis = NSLayoutConstraint.Axis.vertical
        stackView.distribution = .fill
        stackView.alignment = .fill
        stackView.spacing = 10
        stackView.backgroundColor = .gray
        
        var screenData = [ScreenData]()
//        read components from json
        screenData =  loadScreen()
        //print("viewDidLoad screenData : \(screenData)")
        for data in screenData {
            let subView = loadScreenView(data: data, objectId: data.objectid)
            //add components in stack view
            stackView.addArrangedSubview(subView)
        }
        
        self.view.addSubview(stackView)
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
        stackView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 10).isActive = true
        
        stackView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        
        stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
    
// function to laod data from json
    func loadScreen() -> [ScreenData] {
        var jsonData = [ScreenData]()
        if let fileLocation = Bundle.main.url(forResource: "screen_data", withExtension: "json"){
            do{
                let data = try Data(contentsOf: fileLocation)
                let jsonDecoder = JSONDecoder()
                let dataFromJson =  try jsonDecoder.decode([ScreenData].self, from: data)
                jsonData = dataFromJson
            }catch{
                print(error)
            }
        }
        //print("loadScreen screenData :: \(jsonData)")
        return jsonData
    }


Here I check the object type, and depending on that cast the config

    func loadScreenView(data : ScreenData,objectId : String) -> UIView {
        var view = UIView()
        if(objectId == "bd_label"){
            print("bd_label")
            let labelView = UILabel()
            //labelView.sizeToFit()
            let config = data.config as! LabelConfig
            labelView.text = config.label
            labelView.widthAnchor.constraint(equalToConstant: 300).isActive = true
            labelView.heightAnchor.constraint(equalToConstant: 35).isActive = true
            view = labelView
        }
        if(objectId.elementsEqual("bd_button")){
            print("bd_button")
            let buttonView = UIButton()
            let config = data.config as! ButtonConfig
            
            buttonView.setTitle(config.btn_label, for:.normal)
            buttonView.backgroundColor = .blue
            buttonView.widthAnchor.constraint(equalToConstant: 200).isActive = true
            buttonView.heightAnchor.constraint(equalToConstant: 35).isActive = true
            view = buttonView
        }
        if(objectId == "bd_input"){
            print("bd_input")
            let inputView = UITextView()
            let config = data.config as! InputConfig
            
            inputView.text = config.placeholder
            inputView.backgroundColor = .white
            inputView.widthAnchor.constraint(equalToConstant: 300).isActive = true
            inputView.heightAnchor.constraint(equalToConstant: 35).isActive = true
            view = inputView
        }
        
        return view
    }
    

}

【问题讨论】:

  • 您似乎希望解码器通过查看 JSON 来找出您想要的特定类型的 UIConfig。不幸的是,您需要对InputConfigButtonConfigLabelConfig 进行硬编码。
  • 这是否意味着我可以在 struct ScreenData 中使用 var config : UIConfig ?如果没有,还有其他方法可以实现吗?

标签: json swift codable


【解决方案1】:

JSONDecoder 需要知道您想要将 JSON 解码为的具体类型。毕竟,所有东西在运行时都必须有一个具体的类型,你可以通过type(of:) 获得。你不能告诉它只是“解码协议”。编码器有点不同 - 它实际上不需要知道具体类型,并且有一种方法可以绕过它。

似乎UIConfig的类型取决于objectid,所以我们可以检查objectid并决定要解码的UIConfig的类型:

enum CodingKeys: CodingKey {
    case id, objectid, config
}

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    id = try container.decode(String.self, forKey: .id)
    objectid = try container.decode(String.self, forKey: .objectid)
    if objectid == "bd_label" {
        config = try container.decode(LabelConfig.self, forKey: .config)
    } else if objectid == "bd_button" {
        config = try container.decode(ButtonConfig.self, forKey: .config)
    } 
    // other cases...
    else {
        throw DecodingError.dataCorruptedError(forKey: .config, in: container, debugDescription: "no suitable config type found for objectid \(objectid)!")
    } 
}

对于Encodable 部分,您可以制作类似“类型橡皮擦”的东西:

struct AnyEncodable: Encodable {
    let encodeFunction: (Encoder) throws -> Void
    
    init(_ encodable: Encodable) {
        encodeFunction = encodable.encode(to:)
    }
    
    func encode(to encoder: Encoder) throws {
        try encodeFunction(encoder)
    }
}

然后做:

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(id, forKey: .id)
    try container.encode(objectid, forKey: .objectid)
    try container.encode(AnyEncodable(config), forKey: .config)
}

通过使用AnyEncodable,我们基本上将协议包装在一个具体的类型中,但不用担心 - 这实际上不会在 JSON 中创建一对额外的花括号。

【讨论】:

  • 这是完美的解决方案,非常感谢。
  • 如果我将objectid 作为LabelConfigButtonConfig 的一部分,并且我想决定要解码什么类型的UIConfig,有什么办法吗?
  • @ashwini_j 你的意思是objectid 不在ScreenData 中,而是在每个配置对象中?一种方法是在init(decoder:) 中获取配置的嵌套容器。 containerContainer(keyedBy: ConfigCodingKeys.self, forKey: .config) 其中ConfigCodingKeys 有一个键objectid。然后你从嵌套容器中解码objectid 并检查它。如果您可以发布一个新问题,我可以给您更详细的答案。
  • 我已经发布了新问题,这里是链接:stackoverflow.com/questions/67195920/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多