不符合Decodable和Encodable协议
创始人
2024-12-24 13:01:29
0

要使一个类型符合DecodableEncodable协议,它必须能够将其属性值编码为JSON数据或将JSON数据解码为属性值。然而,并非所有类型的属性都可以直接编码或解码为JSON数据。

以下是一些常见的不符合DecodableEncodable协议的情况以及解决方法:

  1. 属性类型不是标准的JSON类型:DecodableEncodable协议要求属性类型必须是标准的JSON类型,例如字符串、数字、布尔值、数组和字典。如果属性类型是自定义类型或非标准的类型,你需要手动实现DecodableEncodable协议中的方法来转换属性值。
struct Person: Decodable, Encodable {
    var name: PersonName // 自定义类型
    var age: Int // 符合标准类型
    
    enum CodingKeys: String, CodingKey {
        case name
        case age
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(PersonName.self, forKey: .name)
        age = try container.decode(Int.self, forKey: .age)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        try container.encode(age, forKey: .age)
    }
}

struct PersonName: Decodable, Encodable {
    var first: String
    var last: String
}
  1. 缺少或多余属性:如果JSON数据中包含了类型中不存在的属性,或者类型中的属性没有在JSON数据中提供对应的值,编码和解码过程将失败。为了解决这个问题,你可以使用CodingKeys枚举来映射属性与JSON键之间的关系。如果你希望忽略某些属性,可以在encode(to:)init(from:)方法中直接忽略它们。
struct Person: Decodable, Encodable {
    var name: String
    var age: Int
    
    enum CodingKeys: String, CodingKey {
        case name = "full_name"
        case age
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        age = try container.decode(Int.self, forKey: .age)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        try container.encode(age, forKey: .age)
    }
}
  1. 属性类型无法编码或解码:有些类型可能无法直接编码或解码为JSON数据,例如日期、URL、枚举等。你可以通过扩展DecodableEncodable协议来提供自定义的编码和解码逻辑。
struct Person: Decodable, Encodable {
    var name: String
    var birthDate: Date // 日期类型
    
    enum CodingKeys: String, CodingKey {
        case name
        case birthDate = "birth_date"
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        let dateString = try container.decode(String.self, forKey: .birthDate)
        // 自定义解码日期逻辑
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        birthDate = dateFormatter.date(from: dateString) ?? Date()
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        // 自定义编码日期逻辑
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateString = dateFormatter.string(from: birthDate)
        try container.encode(dateString, forKey: .birthDate)
    }
}

通过上述方法,你可以解决不符合DecodableEncodable

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...