Swift Decodable Optional Key

前端 未结 3 2133

(This is a follow-up from this question: Using Decodable protocol with multiples keys.)

I have the following Swift code:

let additionalInfo          


        
3条回答
  •  不知归路
    2020-12-09 11:22

    Can you try pasting your sample JSON into quicktype to see what types it infers? Based on your question, I pasted your samples and got:

    struct UserInfo: Codable {
        let firstname: String
        let age: Age?
        let lastname: String
    }
    
    struct Age: Codable {
        let realage: Int?
    }
    

    Making UserInfo.age and Age.realage optionals works, if that's what you're trying to accomplish.

提交回复
热议问题