Error accessing dictionary value in Swift 3

0

I have the following Json

{
   [
          {
            "id": "c200",
            "dia": "Lunes",
            "horario": "07:30/09:20",
            "materia": "Dibujo Tecnico (T)",
            "seccion": "A",
            "profesor" : "Ernesto Arce",
            "aula" : "D1"
          },
          {

            "id": "c383",
            "dia": "Martes",
            "horario": "20:00/21:50",
            "materia": "Sistemas de Telefonia (T)",
            "seccion": "A",
            "profesor" : "-",
            "aula" : "CITEC"

            },
            {

            "id": "c486",
            "dia": "Miercoles",
            "horario": "20:00/21:50",
            "materia": "Materiales 1 (T)",
            "seccion": "A",
            "profesor" : "Gustavo Roman",
            "aula" : "F3"

            }


        ]
}

What I do is convert this Json into a dictionary Array so that I can access the values of each one. I do that with the help of my next code

import UIKit
import Alamofire



extension MockData {

    static func index(completion: @escaping ([MockData]) -> Void) {

        Alamofire.request("https://www.dropbox.com/s/fem028u5ok95270/Clases.json?dl=1") .responseJSON { (response) in
            print(response)
            var users = [MockData]()
            if let objects = response.result.value {
                let json = objects as! NSDictionary
                let list = json["Horario"] as? [[String: AnyObject]]

            for object in list! {
                users.append(MockData(dictionary: object))
            }
            }
            completion(users)

        }

    }
}

import UIKit

class MockData: NSObject {

var id : String?
var dia : DiasClases?
var materia : String?
var horario : String?
var seccion : String?
var profesor : String?
//var aula : String?
//var obs : String?
var HorarioArray = [MockData]()


init(dictionary: [String: AnyObject]){

    self.id = dictionary["id"] as? String
    self.dia = dictionary["dia"] as? DiasClases
    self.materia = dictionary["materia"] as? String
    self.horario = dictionary["horario"] as? String
    self.seccion = dictionary["seccion"] as? String
    self.profesor = dictionary["profesor"] as? String
    //self.aula = dictionary["aula"] as? String
    //self.obs = dictionary["obs"] as? String

      }


   }


enum DiasClases {
case Lunes
case Martes
case Miercoles
case Jueves
case Viernes
case Sabado

static func AllValues() -> [DiasClases] {
    return [Lunes, Martes, Miercoles, Jueves, Viernes, Sabado]
  }
}  

The problem I have is that when debugging my program and verifying that the array is loaded with the correct data, the day item does not contain anything, only says DiasClases? and it does not save me the value of the day. I would greatly appreciate your help. Regards!

    
asked by Silvio Colman 12.09.2017 в 06:56
source

2 answers

1

In the JSON, day is not of type DiaClases, it is of type String. What you have to do is take the day first and then convert to the corresponding DiaClases. In the init take the day and then save it as it should be

let d = dictionary["dia"] as? String
switch d {
   case "Lunes":
       self.dia = .Lunes
   case "Martes":
       self.dia = .Martes
 .....
}
    
answered by 12.09.2017 / 12:16
source
0

Using the following code:

let self.dia = DiasClases(rawValue: dictionary["dia"])!

You get the Enumerator that corresponds to the value you spend with dictionary["dia"] .

To be able to use this method in enumerators with types different from String you must declare the RawValue in the following way:

Example with Int:

First we define what is the RawValue of the enumerators in the following way:

enum DiasClases: Int {
case Lunes = 1
case Martes = 2
case Miercoles = 3
case Jueves = 4
case Viernes = 5
case Sabado = 6
}  

Once you have the RawValue declared, we can use the instruction to find the enumerator of the value you pass:

Example looking for Wednesday:

let self.dia = DiasClases(rawValue: 3)!
    
answered by 12.09.2017 в 09:16