Instantiate object in swift and populate with data from a webapi

0

I have already researched a number of YouTube sites and videos and can not find anything about it, if anyone is willing to show me this code, thank you, because I do not just look at the Apple documentation, I would like an explanation from someone who understands subject and even tell me if it is a good practice to develop something in this line of reasoning

I have a webapi that returns a list of tests in JSON:

{
 "Provas": [
{
  "Codigo": "16505984073",
  "Disciplina": {
    "Codigo": "657060",
   "Nome": "DISCIPLINA 1"
  },
  "Tipo": "SUB",
  "Correcao": "2016-07-01T14:33:33.383",
 },
{
  "Codigo": "16994242303",
  "Disciplina": {
    "Codigo": "652640",
    "Nome": "DISCIPLINA 2"
  },
  "Tipo": "SUB",
  "Correcao": "2016-06-30T11:53:11.207",
 },
{
  "Codigo": "16916014662",
  "Disciplina": {
    "Codigo": "652540",
    "Nome": "DISCIPLINA 3"
  },
  "Tipo": "BIMESTRAL",
  "Correcao": "2016-06-29T09:42:29.097",
 },
{
  "Codigo": "16892010587",
  "Disciplina": {
    "Codigo": "656140",
   "Nome": "DISCIPLINA 4"
  },
  "Tipo": "BIMESTRAL",
  "Correcao": "2016-06-25T14:49:57.17",
 },
{
  "Codigo": "16435696693",
  "Disciplina": {
    "Codigo": "611460",
    "Nome": "DISCIPLINA 5"
  },
  "Tipo": "BIMESTRAL",
  "Correcao": "2016-05-04T15:42:08.363",
 },
{
  "Codigo": "16781197682",
  "Disciplina": {
    "Codigo": "567140",
    "Nome": "DISCIPLINA 6"
  },
  "Tipo": "SUB",
  "Correcao": "2016-05-04T15:38:09.707",
 },
{
  "Codigo": "16496847758",
  "Disciplina": {
    "Codigo": "554740",
    "Nome": "DISCIPLINA 7"
  },
  "Tipo": "SUB",
  "Correcao": "2016-05-03T15:45:38.553",
 },
{
  "Codigo": "16640802740",
  "Disciplina": {
    "Codigo": "538740",
    "Nome": "DISCIPLINA 8"
  },
  "Tipo": "SUB",
  "Correcao": "2016-05-03T14:50:06.06",
 }
  ]
}

And I have a Proof class

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 calls Discipline

import Foundation

class Disciplina
{
    let codigo:String
    let nomedisciplina:String

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

I would like to populate this wepapi return by instantiating my "Proof" object and then popping a table up until I could do this by getting the JSON data using the '' Alamofire '' and populating my object with that data using 'ObjectMapper' ', but I would like to know if there is anything native to Swift 3.0 because now with the update of the language' 'Alamofire' 'and' 'ObjectMapper' 'are giving many errors and also I do not think trivial depend on solutions. I also do not know if this would be the good practice for working with web data (returning data in '' JSON '' and converting it to Object), otherwise it could show me what code it would be for '' Swift 3.0 '', I have the '' Xcode 8 '' already.

    
asked by anonymous 22.09.2016 / 17:24

1 answer

1

A good API to do this is EVReflection.

link

The master branch of it is for Swift 3.0 already and is extremely easy to do the conversion, just put in the header of the class that it is an EVObject and then use the constructor.

class Prova : EVObject
{
    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
    }
}

Then you only use the builder:

let jsonString = respostaDoSeuWebServer()
let provaObj = Prova(json: jsonString)

If you have more questions, here is a good tutorial, in less than 5 minutes teaches you to do both GET request and conversion: link

    
28.10.2016 / 20:58