Populate object in Swift using data from a JSON Return

Asked

Viewed 390 times

0

I have a webapi that returns a list of evidence:

{
  "Provas": [
    {
      "Codigo": "11111111111",
      "Disciplina": {
        "Codigo": "111111",
        "Nome": "SOCIOLOGIA E EDUCAÇÃO"
      },
      "Tipo": "SUB",
      "Correcao": "2016-07-01T14:33:33.383",
      "Exame": false,
      "Sub": true
    },
    {
      "Codigo": "22222222222",
      "Disciplina": {
        "Codigo": "333333",
        "Nome": "EDUCAÇÃO DE JOVENS E ADULTOS: FUNDAMENTOS E METODOLOGIA"
      },
      "Tipo": "SUB",
      "Correcao": "2016-06-30T11:53:11.207",
      "Exame": false,
      "Sub": true
    },
    {
      "Codigo": "333333333333",
      "Disciplina": {
        "Codigo": "444444",
        "Nome": "METODOLOGIA E PRÃTICA DO ENSINO DA HISTÓRIA E GEOGRAFIA"
      },
      "Tipo": "BIMESTRAL",
      "Correcao": "2016-06-29T09:42:29.097",
      "Exame": false,
      "Sub": false
    }
  ]
}

And I created a class called Proof

import Foundation

class Prova
{
    let codigo:String
    let tipo:String
    let dtcorrecao:NSDate
    let disciplina:Disciplina

    init(codigo:String, tipo:String, dtcorrecao:NSDate, disciplina:Disciplina)
    {
        self.codigo = codigo
        self.tipo = tipo
        self.dtcorrecao = dtcorrecao
        self.disciplina = disciplina
    }
}

and another call Discipline

import Foundation

class Disciplina
{
    let codigo:String
    let nomedisciplina:String

    init(codigo:String, nomedisciplina:String)
    {
        self.codigo = codigo
        self.nomedisciplina = nomedisciplina
    }
}

Would you like to know how to convert this data to a list of Proofs according to my class? , I need to change something in the construction of these classes?

1 answer

0


An option using open source framework is Object Mapper:

import Foundation
import ObjectMapper //importa o framework

class Prova: Mappable // adota o protocolo
{
    // declara as variaveis
    var codigo: String!
    var tipo: String!
    var dtcorrecao: NSDate!
    var disciplina: Disciplina!
    var sub: Bool!
    var exame: Bool!

    // chama o inicializador que mapeia o JSON
    required init?(_ map: Map) {}

    // mapeia a classe conforme os campos do JSON
    func mapping(map: Map) {
      codigo      <- map["Codigo"]
      tipo        <- map["Tipo"]
      dtcorrecao  <- map["Correcao"]
      disciplina  <- map["Disciplina"]
      sub         <- map["Sub"]
      exame       <- map["Exame"]
    }
}

Discipline:

import Foundation
import ObjectMapper

class Disciplina: Mappable
{
    var codigo: String!
    var nomedisciplina: String!

    required init?(_ map: Map) {}

    func mapping(map: Map) {
       codigo           <- map["Codigo"]
       nomedisciplina   <- map["Nome"]
    }
}

Proofs: (we need to map this key at the top of JSON)

import Foundation
import ObjectMapper

class Provas: Mappable
{
    var provas: [Prova]!

    required init?(_ map: Map) {}

    func mapping(map: Map) {
       provas           <- map["Provas"]
    }
}

To create a mapped object:

After you have requested JSON just call

let provas = Mapper<Provas>().map(JSONString)
  • Dude worked great thanks.

  • But how would it look inside a Tableviewcontrollor?? , I created a function to call this data via Alamofire, I did all the table view code but do not click on the tableview

  • As you already have the array of Provas just run it through the Tableviewcontroller in case you’re not on it and call the reloadData():

  • I was forgetting about the timeData(), now it worked out thanks!!!

  • How do I save this Proof object in a session (Nsuserdefaults)

  • I have a little different situation my json doesn’t have a title on top it already comes this way [{ "id": "1", "Description": "TEST 1", "live": "https://www.example1.com/live", "Thumb": "https://www.example1.com/thumb", "online": true }, {"id": "2", "Description": "TEST 2", "live": "https://www.exe2amplcom.live/", "Thumb": "https://www.example2.com/thumb", "online": true }]

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.