Instantiate Swift object and populate with webapi data

Asked

Viewed 501 times

0

I’ve searched several Youtube sites and videos and I can’t find anything about it, if there’s anyone willing to show me that code thank you, because I can’t just look at Apple’s documentation, would like an explanation from someone who understands the 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 proofs 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 got a 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 call 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 fill this wepapi return instantiating my "Proof" object and then populate a table until I was able to do that by receiving the data from 'JSON' using 'Alamofire' and populating my object with this data using ''Objectmapper', but I wonder if there is something native to ''Swift 3.0'' because now with the update of the language '''Alamofire'' and ''Objectmapper' are giving many errors and also do not find it trivial to depend on third party solutions. I also do not know if this would be the good practice to work with web data (Return data in ''JSON' and convert to Object), if they could not show me what would be in code for ''Swift 3.0', I’m with ''Xcode 8'.

1 answer

1

A good API to do this is Evreflection.

https://github.com/evermeer/EVReflection

Her master branch is for Swift 3.0 now and is extremely easy to convert, just put in the class header 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 construction company:

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

If you have more questions, here is a good tutorial, in less than 5 minutes teaches to do both the GET request and the conversion: https://www.youtube.com/watch?v=LPWsQD2nxqg

Browser other questions tagged

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