Swift Codable实践

背景:项目原本是使用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)
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,458评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,030评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,879评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,278评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,296评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,019评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,633评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,541评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,068评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,181评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,318评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,991评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,670评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,183评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,302评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,655评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,327评论 2 358

推荐阅读更多精彩内容