不符合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

相关内容

热门资讯

iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
oppo手机安卓系统换成苹果系... OPPO手机安卓系统换成苹果系统:现实吗?如何操作?随着智能手机市场的不断发展,用户对于手机系统的需...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...
安卓平板改windows 系统... 你有没有想过,你的安卓平板电脑是不是也能变身成Windows系统的超级英雄呢?想象在同一个设备上,你...
安卓系统上滑按键,便捷生活与高... 你有没有发现,现在手机屏幕越来越大,操作起来却越来越方便了呢?这都得归功于安卓系统上的那些神奇的上滑...
安卓系统连接耳机模式,蓝牙、有... 亲爱的手机控们,你们有没有遇到过这种情况:手机突然变成了“耳机模式”,明明耳机没插,声音却只从耳机孔...
希沃系统怎么装安卓系统,解锁更... 亲爱的读者们,你是否也像我一样,对希沃一体机上的安卓系统充满了好奇呢?想象在教室里,你的希沃一体机不...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...