Working with json and class

I have a web api that returns a certain 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", 
}
] 
}


I created a class named "Prova"


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 name "Disciplina"


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 complete this return wepapi instancing my object "Proof" and then populate a table without using "Alamofire" and "ObjectMapper," I wonder if there is something native '' Swift 3.0 ''. I do not know if this would be good practice to work with web data (return data in '' JSON '' and convert to object), if not they could show me what would be in code for '' Swift 3.0 '', I am the '' Xcode 8 '' already. I need to change or add something in my classes?

Accepted Reply

There’s an official Swift blog post that covers this.

FYI, there’s really two schools of thought here:

  • Do something straightforward, as shown by that blog post.

  • Do something general, as shown by the many different Swift / JSON libraries you’ll find out on the ’net.

Which approach you should use really depends on your requirements. If I were dealing with a single, relatively simple web service request — like the example you posted — I would use the straightforward approach. OTOH, if I were dealing with a complex web services API with hundreds of different requests, I’d invest in some infrastructure. Between those extremes lies a tipping point that depends on your experience, company policy, personality, and so on.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

There’s an official Swift blog post that covers this.

FYI, there’s really two schools of thought here:

  • Do something straightforward, as shown by that blog post.

  • Do something general, as shown by the many different Swift / JSON libraries you’ll find out on the ’net.

Which approach you should use really depends on your requirements. If I were dealing with a single, relatively simple web service request — like the example you posted — I would use the straightforward approach. OTOH, if I were dealing with a complex web services API with hundreds of different requests, I’d invest in some infrastructure. Between those extremes lies a tipping point that depends on your experience, company policy, personality, and so on.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"