when trying to decode the return of the array of objects "arquivo", always nil
import UIKit
struct pessoa: Codable {
let idPessoa: Int
let nome: String
let cpf: String
let email: String
let dtaNascimento: String
let dtaCadastro: String
let telefone: String
let status: Int
}
struct arquivo: Codable {
let idArquivo: Int
let caminho: String
let nome: String
let descricao: String
let tamanho: Double
let idPessoa: Int
let idOcorrencia: Int
enum CodingKeys: String, CodingKey {
case idArquivo
case caminho
case nome
case descricao
case tamanho
case idPessoa
case idOcorrencia
}
}
struct PessoaDTO: Codable {
let pessoa: pessoa!
let arquivo: Array<arquivo>!
enum CodingKeys: String, CodingKey {
case pessoa = "pessoa"
case arquivo = "arquivo"
}
}
var json = """
{
"pessoa":
{
"idPessoa": 89,
"nome": "Arnaldo",
"cpf": "816.404.648-44",
"email": "testeocorrenciabus@google.com",
"dtaNascimento": "1969-01-01T03:00:00.000+0000",
"dtaCadastro": "2019-10-30T18:53:19.000+0000",
"telefone": "(54)58648-4464",
"status": 1,
"arquivo": [
{ "idArquivo": 91,
"caminho": "/images/u/1572461505454.jpg",
"nome": "1572461505454.jpg",
"descricao": "",
"tamanho": 31765,
"idPessoa": 89,
"idOcorrencia": 1
}
]
}
}
"""
do {
let decodedBeerObject = try JSONDecoder().decode(PessoaDTO.self, from: json.data(using: .utf8)!)
print(decodedBeerObject.pessoa)
print(decodedBeerObject.arquivo)
} catch let error {
print(error.localizedDescription)
}
Result:
Optional(__lldb_expr_51.pessoa(idPessoa: 89, nome: "Arnaldo", cpf: "816.404.648-44", email: "testeocorrenciabus@google.com", dtaNascimento: "1969-01-01T03:00:00.000+0000", dtaCadastro: "2019-10-30T18:53:19.000+0000", telefone: "(54)58648-4464", status: 1)) nil
Your Model is incorrect
arquivo
is exist inside the pessoa
not in a same level.
Correct parsing code is:
struct Pessoa: Codable {
let idPessoa: Int
let nome: String
let cpf: String
let email: String
let dtaNascimento: String
let dtaCadastro: String
let telefone: String
let status: Int
let arquivo: Array<Arquivo>!
}
struct Arquivo: Codable {
let idArquivo: Int
let caminho: String
let nome: String
let descricao: String
let tamanho: Double
let idPessoa: Int
let idOcorrencia: Int
}
struct PessoaDTO: Codable {
let pessoa: Pessoa!
}
var json123 = """
{
"pessoa":
{
"idPessoa": 89,
"nome": "Arnaldo",
"cpf": "816.404.648-44",
"email": "testeocorrenciabus@google.com",
"dtaNascimento": "1969-01-01T03:00:00.000+0000",
"dtaCadastro": "2019-10-30T18:53:19.000+0000",
"telefone": "(54)58648-4464",
"status": 1,
"arquivo": [
{ "idArquivo": 91,
"caminho": "/images/u/1572461505454.jpg",
"nome": "1572461505454.jpg",
"descricao": "",
"tamanho": 31765,
"idPessoa": 89,
"idOcorrencia": 1
}
]
}
}
"""
do {
let decodedBeerObject = try JSONDecoder().decode(PessoaDTO.self, from: json123.data(using: .utf8)!)
print(decodedBeerObject.pessoa)
print(decodedBeerObject.pessoa.arquivo)
} catch let error {
print(error.localizedDescription)
}
Your structs are slightly wrong, the key arquivo
is a part of Pessoa
struct Pessoa: Codable {
let idPessoa: Int
let nome: String
let cpf: String
let email: String
let arquivo: [Arquivo]
let dtaNascimento: String
let dtaCadastro: String
let telefone: String
let status: Int
}
struct Arquivo: Codable {
let idArquivo: Int
let caminho: String
let nome: String
let descricao: String
let tamanho: Double
let idPessoa: Int
let idOcorrencia: Int
}
struct PessoaDTO: Codable {
let pessoa: Pessoa
}
Codingkeys are not needed if the name of the key and the struct member matches
To get the real error print always only the error
instance
catch {
print(error)
}
And please name structs with starting capital letter.
And never declare struct members in a Codable
context as implicit unwrapped optional .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.