美文网首页
Swift Codable实践

Swift Codable实践

作者: OrrHsiao | 来源:发表于2023-04-10 15:13 被阅读0次

背景:项目原本是使用HandyJson来做的数据-模型的相互转换,后面测试发现在iOS12.1系统的,HandyJson库会必现崩溃libswiftCoreGraphics image not found,该库作者已经不再更新且不推荐使用了,具体详细原及可以看下我之前的文章:https://www.jianshu.com/p/167167f54a0d

后面就考虑移除HandyJson改为系统Codable去实现数据-模型的相互转换,从Handyjson迁移到Codable的过程中,我发现了如下问题

  1. 我发现HandyJson在做转换时,如果json数据的某个字段的类型与model类的对应的字段类型不匹配时,HandyJson会尝试自动帮你进行类型转换,但是在Codable中,类型不匹配会导致错误,换句话说就是必须保证jsonObjct中对应的属性名称是对应的,否则该属性的自动会转换失败,如果想要转换,必须手动实现codable对应的方法。

  2. 对于继承类型,无法自动转换该类型的父类或子类属性,如果想要转换,必须手动实现codable对应的方法。

  3. 系统对于实体类属性,会根据是可选值或不可选值自动调用对应的方法,如果json这个key为空,但是实体类属性为不可选值,这也会导致对象转换失败。

这几点差异,导致对于每个改动的接口,都需要进行一番调试,查看转换是否正常,耗费了很多时间。

以下为实践过程中总结的一些经验:
// 遵循codable协议,即可自动转化,前提:
// 1.类的父类是NSobject或空
// 2.类的属性类型与json或者jsonObjct中对应的类型是对应的,否则对象会转化失败
// 3.类的属性名称与json或者jsonObjct中对应的属性名称是对应的,否则该属性会转换失败
// 4.如果包含嵌套类型,则嵌套类型也需要实现codable协议
// 5.系统对于实体类属性,会根据是可选值或不可选值自动调用对应的方法,如果json这个key为空,但是实体类属性为不可选值,这也会导致对象转换失败

self.childName = try container.decodeIfPresent(String.self, forKey: .childName)
self.childName = try container.decode(String.self, forKey: .childName)
try cotainer.encodeIfPresent(self.childName, forKey: .childName)
try cotainer.encode(self.childName, forKey: .childName)

// 无法自动转化时
// 1.类如果是子类,需要手动实现enum CodingKeys:/ init(from decoder: Decoder)/ encode(to encoder: Encoder)方法,父类需要遵守codable协议,但可以自动转化
// 2.类的属性类型不对应,需要手动实现enum CodingKeys:/ init(from decoder: Decoder)/ encode(to encoder: Encoder)方法
// 3.类的属性名称不对应,需要手动实现enum CodingKeys,所有属性都必须写出来

public class JsonToModelViewController: UIViewController {
    
    /*
    let jsonStr = """
            {
                "token_expires_timestamp":1680680304281,
                "uid":"4478366175",
                "token_expires_time":86400,
                "isSetPwd":"0",
                "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODA2ODAzMDQyODEsInBheWxvYWQiOiJ7XCJpbWlVaWRcIjpcIjQ0NzgzNjYxNzVcIixcImZsYWdcIjoxfSJ9.KOBDBhs_pnDPdp85UOebQ6lfJy_Tf_w-YhXj0KlTyok",
                "refresh_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODE4MDM1MDQyODEsInBheWxvYWQiOiJ7XCJpbWlVaWRcIjpcIjQ0NzgzNjYxNzVcIixcImZsYWdcIjoyfSJ9.0AJs5VFJjIOfOG3VjoJ9zx75taRS2xFGbrpEamyWoAk",
                "refresh_token_expires_timestamp":1681803504281,
                "authCode":"0167247243702923",
                "refresh_token_expires_time":1209600,
                "currentUtcDate":1680593904281,
            }
    """
     */
    let jsonStr = """
    {
            "name": "张三",
            "age": 20,
            "description": "A handsome boy.",
            "childName": "小a"
        }
    """
    let jsonLabel = UILabel(frame: CGRectZero)
    let jsonToModelButton = UIButton(type: .system)
    let modelToJsonButton = UIButton(type: .system)
    let modelToJsonObjButton = UIButton(type: .system)
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        initViews()
    }
    
    func initViews() {
        self.navigationItem.title = "json转模型"
        self.view.backgroundColor = .white
        
        jsonLabel.text = jsonStr
        jsonLabel.numberOfLines = 0
        jsonLabel.adjustsFontSizeToFitWidth = true
        self.view.addSubview(jsonLabel)
        
        jsonToModelButton.setTitle("json转model", for: .normal)
        jsonToModelButton.backgroundColor = .gray
        jsonToModelButton.addTarget(self, action: #selector(jsonToModel), for: .touchUpInside)
        self.view.addSubview(jsonToModelButton)
        
        modelToJsonButton.setTitle("model转json", for: .normal)
        modelToJsonButton.backgroundColor = .gray
        modelToJsonButton.addTarget(self, action: #selector(modelToJson), for: .touchUpInside)
        self.view.addSubview(modelToJsonButton)
        
        modelToJsonObjButton.setTitle("model转jsonObj", for: .normal)
        modelToJsonObjButton.backgroundColor = .gray
        modelToJsonObjButton.addTarget(self, action: #selector(modelToJsonObj), for: .touchUpInside)
        self.view.addSubview(modelToJsonObjButton)
    }
    
    public override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        jsonLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(64)
            make.height.lessThanOrEqualTo(300)
        }
        
        jsonToModelButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(jsonLabel.snp.bottom).offset(10)
            make.height.equalTo(44)
        }
        
        modelToJsonButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(jsonToModelButton.snp.bottom).offset(10)
            make.height.equalTo(44)
        }
        
        modelToJsonObjButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(modelToJsonButton.snp.bottom).offset(10)
            make.height.equalTo(44)
        }
    }
    
    @objc private func jsonToModel() {
        do {
//            let model = try JSONDecoder().decode(ILAuthInfo.self, from: jsonStr.data(using: .utf8)!)
//            let model = try JSONDecoder().decode(ILTestModel.self, from: jsonStr.data(using: .utf8)!)
            let model = try JSONDecoder().decode(ILTestChildModel.self, from: jsonStr.data(using: .utf8)!)
            print("转换得到的模型:\(model)")
        }catch {
            imiLogE("转换失败:\(error)")
        }
        
        let jsonStr = """
        {"total":2,"cameraCount":1,"items":[{"name":"cm1"},{"name":"cm2"}]}
        """
        do {
            let model = try JSONDecoder().decode(ILHomeRoomModel.self, from: jsonStr.data(using: .utf8)!)
            print("转换得到的模型:\(model)")
        }catch {
            imiLogE("转换失败:\(error)")
        }
    }
    
    @objc private func modelToJson() {
        let m1 = ILTestModel()
        m1.name = "张三"
        let m2 = ILTestModel()
        m2.name = "李四"
        do {
            let jsonData = try JSONEncoder().encode([m1, m2])
            let json = String(data: jsonData, encoding: .utf8)
            print("转换得到的json1:\(json ?? "空")")
        }catch{
            imiLogE("转换失败:\(error)")
        }
        
        let m = ILHomeRoomModel()
        m.total = 2
        m.cameraCount = 1
        let cm1 = ILRoomItem()
        cm1.name = "cm1"
        let cm2 = ILRoomItem()
        cm2.name = "cm2"
        m.items = [cm1, cm2]
        do {
            let jsonData = try JSONEncoder().encode(m)
            let json = String(data: jsonData, encoding: .utf8)
            print("转换得到的json2:\(json ?? "空")")
        }catch{
            imiLogE("转换失败:\(error)")
        }
        
        let bm = ILTestBigModel()
        bm.bigName = "大名"
        bm.child = m2
        do {
            let jsonData = try JSONEncoder().encode(bm)
            let json = String(data: jsonData, encoding: .utf8)
            print("转换得到的json3:\(json ?? "空")")
        }catch{
            imiLogE("转换失败:\(error)")
        }
    }
    
    @objc private func modelToJsonObj() {
        let model = ILTestModel()
        model.name = "李四"
        model.smallName = "a"
        model.des = "啊哈"
        model.age = 22
        
        let jsonObj = ILCodableUtil.modelToJsonObject(model) as? [String: Any]
        
    }
}

@objcMembers
public class ILTestModel: NSObject, Codable {
    var name: String?
    var smallName: String?
    var des: String?
    var age: Int = 0
    
    public override var description: String {
        "name:\(name ?? "空"), smallName:\(smallName ?? "空"), des:\(des ?? "空"), age:\(age)"
    }
    
//    enum CodingKeys: String, CodingKey {
//        case name
//        case smallName
//        case des = "description"
//        case age
//    }
    
    //手动实现
    /*
    public required init(from decoder: Decoder) throws {
        super.init()
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.nickName = try container.decodeIfPresent(String.self, forKey: .nickName)
        self.deviceName = try container.decodeIfPresent(String.self, forKey: .deviceName)
        self.mac = try container.decodeIfPresent(String.self, forKey: .mac)
        self.productKey = try container.decodeIfPresent(String.self, forKey: .productKey)
        self.token = try container.decodeIfPresent(String.self, forKey: .token)
        self.categoryKey = try container.decodeIfPresent(String.self, forKey: .categoryKey)
        //特殊处理一下字典
        let dicData = try container.decodeIfPresent(Data.self, forKey: .extraDeviceInfo)
        self.extraDeviceInfo = ILLocalDeviceKeychainUtil.dataToDictionary(dicData);
    }

    public func encode(to encoder: Encoder) throws {
        //super.encode(to: encoder)
        var cotainer = encoder.container(keyedBy: CodingKeys.self)
        try cotainer.encodeIfPresent(self.nickName, forKey: .nickName)
        try cotainer.encodeIfPresent(self.deviceName, forKey: .deviceName)
        try cotainer.encodeIfPresent(self.mac, forKey: .mac)
        try cotainer.encodeIfPresent(self.productKey, forKey: .productKey)
        try cotainer.encodeIfPresent(self.token, forKey: .token)
        try cotainer.encodeIfPresent(self.categoryKey, forKey: .categoryKey)
        //特殊处理一下字典
        if let dicData = ILLocalDeviceKeychainUtil.dictionaryToData(self.extraDeviceInfo) {
            try cotainer.encodeIfPresent(dicData, forKey: .extraDeviceInfo)
        }
    }
     */
}

@objcMembers public class ILTestBigModel: NSObject, Codable {
    var bigName: String?
    var child: ILTestModel?
}

/// 子类想要使用Codable转换,必须手写以下实现
/// 与现有的 NSCoding API (NSKeyedArchiver) 不同,为了灵活性和安全性,新的 Swift 4 Codable 实现不会将有关编码类型的类型信息写入生成的档案中。因此,在解码时,API 只能使用您提供的具体类型来解码值(在您的情况下是超类类型)。
@objcMembers public class ILTestChildModel: ILTestModel {
    var childName: String?
    
    enum CodingKeys: String, CodingKey {
        case childName = "childName"
    }
    
    public required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.childName = try container.decodeIfPresent(String.self, forKey: .childName)
    }
    
    public override func encode(to encoder: Encoder) throws {
        var cotainer = encoder.container(keyedBy: CodingKeys.self)
        try cotainer.encodeIfPresent(self.childName, forKey: .childName)
    }
}

相关文章

网友评论

      本文标题:Swift Codable实践

      本文链接:https://www.haomeiwen.com/subject/ymqwddtx.html