可编码的自定义类类型属性不能从 JSON 初始化它自己的属性

Posted

技术标签:

【中文标题】可编码的自定义类类型属性不能从 JSON 初始化它自己的属性【英文标题】:Codable custom class type property can not initialize its own properties from JSON 【发布时间】:2018-02-06 09:39:06 【问题描述】:

我的 Json:

      
   "message":"OK",
   "response":[  
        
         "article_id":"201802062200722818",
         "lead":"Poliisi vapautti naisen ja otti miehen kiinni. Satakunnan käräjäoikeus vangitsi miehen tiistaina.",
         "headline":"Poliisi epäilee: 19-vuotias raumalaismies piti alaikäistä naista mökillä vankina",
         "title":"Poliisi epäilee: 19-vuotias raumalaismies piti alaikäistä naista mökillä vankina",
         "service_name":"iltalehti",
         "main_image_name":"cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
         "category":  
            "category_name":"kotimaa",
            "description":"Kotimaan uutiset",
            "parent_category":  
               "category_name":"uutiset",
               "description":"Uutiset",
               "parent_category":null
            
         ,
         "main_image_urls":  
            "default":"https://img.ilcdn.fi/PcWFp0weItXN2WAWKBXCO_H2VsQ=/510x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size30":"https://img.ilcdn.fi/_LNHr84u93ntg3tX37oHyGBlRNA=/30x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size98":"https://img.ilcdn.fi/r624bQFqaJ3xqrMScif38JH6SBM=/98x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size138":"https://img.ilcdn.fi/dyemZCdMpjAFTnnD5JiYLh3WGJI=/138x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size244":"https://img.ilcdn.fi/2AiJpLa4oLxEDE0jL_LazhOiTMM=/244x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size293":"https://img.ilcdn.fi/iyAZVQ0ufAHrX2inGCiE9QPQjMU=/293x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size310":"https://img.ilcdn.fi/XGmL7EEqo0OR5Vzvbel1hSeTmHI=/310x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size510":"https://img.ilcdn.fi/PcWFp0weItXN2WAWKBXCO_H2VsQ=/510x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size820":"https://img.ilcdn.fi/N-XV5ZqQASGpvUe-3DAcq4i1928=/820x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg",
            "size1024":"https://img.ilcdn.fi/J5Cm5P2SJMNymHza7s3LdEEvKLg=/1024x/img-s3.ilcdn.fi/cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg"
         ,
         "published_at":"2018-02-06T10:42:40+02:00",
         "updated_at":null
      
   ]

我的Codable 模型类

Articles.swift

import Foundation
struct Articles : Codable 
    let message : String?
    let response : [Article]?

    enum CodingKeys: String, CodingKey 

        case message = "message"
        case response = "response"
    

    init(from decoder: Decoder) throws 
        let values = try decoder.container(keyedBy: CodingKeys.self)
        message = try values.decodeIfPresent(String.self, forKey: .message)
        response = try values.decodeIfPresent([Article].self, forKey: .response)
    


Article.swift

import Foundation
struct Article : Codable 
    let article_id : String?
    let lead : String?
    let headline : String?
    let title : String?
    let service_name : String?
    let main_image_name : String?
    let category : Category?
    let main_image_urls : Main_image_urls?
    let published_at : String?
    let updated_at : String?

    enum CodingKeys: String, CodingKey 

        case article_id = "article_id"
        case lead = "lead"
        case headline = "headline"
        case title = "title"
        case service_name = "service_name"
        case main_image_name = "main_image_name"
        case category
        case main_image_urls
        case published_at = "published_at"
        case updated_at = "updated_at"
    

    init(from decoder: Decoder) throws 
        let values = try decoder.container(keyedBy: CodingKeys.self)
        article_id = try values.decodeIfPresent(String.self, forKey: .article_id)
        lead = try values.decodeIfPresent(String.self, forKey: .lead)
        headline = try values.decodeIfPresent(String.self, forKey: .headline)
        title = try values.decodeIfPresent(String.self, forKey: .title)
        service_name = try values.decodeIfPresent(String.self, forKey: .service_name)
        main_image_name = try values.decodeIfPresent(String.self, forKey: .main_image_name)
        category = try Category(from: decoder)
        main_image_urls = try Main_image_urls(from: decoder)
        published_at = try values.decodeIfPresent(String.self, forKey: .published_at)
        updated_at = try values.decodeIfPresent(String.self, forKey: .updated_at)
    


Category.swift

import Foundation
struct Category : Codable 
    let category_name : String?
    let description : String?
    let parent_category : Parent_category?

    enum CodingKeys: String, CodingKey 

        case category_name = "category_name"
        case description = "description"
        case parent_category
    

    init(from decoder: Decoder) throws 
        let values = try decoder.container(keyedBy: CodingKeys.self)
        category_name = try values.decodeIfPresent(String.self, forKey: .category_name)
        description = try values.decodeIfPresent(String.self, forKey: .description)
        parent_category = try Parent_category(from: decoder)
    


Parent_category.swift

import Foundation
struct Parent_category : Codable 
    let category_name : String?
    let description : String?
    let parent_category : String?

    enum CodingKeys: String, CodingKey 

        case category_name = "category_name"
        case description = "description"
        case parent_category = "parent_category"
    

    init(from decoder: Decoder) throws 
        let values = try decoder.container(keyedBy: CodingKeys.self)
        category_name = try values.decodeIfPresent(String.self, forKey: .category_name)
        description = try values.decodeIfPresent(String.self, forKey: .description)
        parent_category = try values.decodeIfPresent(String.self, forKey: .parent_category)
    


问题:

我正在尝试使用Codable 协议从 JSON 初始化我的模型类。它适用于原生数据类型(String、Int 等),但如果结构包含自定义类型属性对象,则不会初始化该自定义结构(类)的属性。

示例:CategoryArticle 结构中的自定义类型对象。每个自定义类负责初始化其属性,确认Codable 协议并拥有自己的init(from decoder: Decoder) 方法。

但不知何故,category 和其他自定义类型无法初始化它们自己的属性。(例如,Category 类中的 category_name = nilParent_categoryMain_image_urls 也是如此),我正在关注结果:(有些值为零)

xcode 上的控制台登录

po article

▿ Optional<Article>
  ▿ some : Article
    ▿ article_id : Optional<String>
      - some : "201802062200722818"
    ▿ lead : Optional<String>
      - some : "Poliisi vapautti naisen ja otti miehen kiinni. Satakunnan käräjäoikeus vangitsi miehen tiistaina."
    ▿ headline : Optional<String>
      - some : "Poliisi epäilee: 19-vuotias raumalaismies piti alaikäistä naista mökillä vankina"
    ▿ title : Optional<String>
      - some : "Poliisi epäilee: 19-vuotias raumalaismies piti alaikäistä naista mökillä vankina"
    ▿ service_name : Optional<String>
      - some : "iltalehti"
    ▿ main_image_name : Optional<String>
      - some : "cae51c694cc7f31257290ff96489f3cf852329a887509621b29a27fbaa0f8894.jpg"
    ▿ category : Optional<Category>
      ▿ some : Category
        - category_name : nil
        - description : nil
        ▿ parent_category : Optional<Parent_category>
          ▿ some : Parent_category
            - category_name : nil
            - description : nil
            - parent_category : nil
    ▿ main_image_urls : Optional<Main_image_urls>
      ▿ some : Main_image_urls
        - default : nil
        - size30 : nil
        - size98 : nil
        - size138 : nil
        - size244 : nil
        - size293 : nil
        - size310 : nil
        - size510 : nil
        - size820 : nil
        - size1024 : nil
    ▿ published_at : Optional<String>
      - some : "2018-02-06T10:42:40+02:00"
    - updated_at : nil

我错过了什么吗?请帮助大家:-)

【问题讨论】:

如果在 Article.swift 中的 category = try Category(from: decoder) 处设置断点会发生什么?是否有一个错误可以告诉您失败的原因? 没有任何反应。没有错误。行 try values.decodeIfPresent(String.self, forKey: .category_name) 在我的类别类的 init 方法中返回 nil 【参考方案1】:

Article.swift 尝试更改以下行

category = try Category(from: decoder)
main_image_urls = try Main_image_urls(from: decoder)

category = values.decodeIfPresent(Category.self, forKey: .category)
main_image_urls = values.decodeIfPresent(Main_image_urls.self, forKey: .main_image_urls)

您的类Main_image_urls 也应该符合可编码。

您也可以尝试省略init(from decoder: Decoder) 方法,让编译器合成它。这也适用于自定义 CodingKeys 枚举。

更新:

同时更改Category.swift中的以下行

parent_category = try Parent_category(from: decoder)

parent_category = values.decodeIfPresent(Parent_category.self, forKey: .parentCategory)

【讨论】:

我试过了。它抛出错误 Error Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode String but found a dictionary 相反。" UserInfo=NSCodingPath=( "IltalehtiToday.Articles.CodingKeys.response", "Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: \"Index 0\", intValue: Optional(0))", "IltalehtiToday.Article.CodingKeys. category", "IltalehtiToday.Parent_category.CodingKeys.parent_category" ), NSDebugDescription=应该解码 String 但找到了字典。 从数据中解析 JSON (size:2391) 我更新了我的答案:-)。如果这没有帮助,请尝试删除每个结构中的初始化函数。 Xcode 会自己生成它,因为你所有的结构都是可编码的。 啊,好的,它现在正在工作。谢谢。你知道为什么不使用 category = try Category(from: decoder) 是的,如果不使用 values.decode... 你在这个函数里面,解码器不知道在哪里(在哪个键)寻找指定的对象。

以上是关于可编码的自定义类类型属性不能从 JSON 初始化它自己的属性的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自定义类型属性从 JSON 初始化模型类

在 swift 4 中使用可编码的 JSON 时出错?

初始化期间可编码的默认值

用于深层嵌套对象的自定义Json Serializer

基于属性数量的自定义 JSON 格式

在符合 UIApperance 的自定义类中允许哪些 Swift 属性类型?