0 votes

Error accessing Dictionary value in Swift 3

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 to convert this Json into a dictionary Array so I can access the values of each one. I do that with the help of my following 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
            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))



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 I debug my program and verify that the array is loaded with the correct data, the item day does not contain anything, it only says DiasClases? and it does not save the value of the day. I would appreciate your help. Greetings!


Spidvmp Points 1010

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

let d = dictionary["dia"] as? String
switch d {
   case "Lunes":
       self.dia = .Lunes
   case "Martes":
       self.dia = .Martes


Marc Points 3265

Using the following code:

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

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

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

Example with Int:

First we define which is the RawValue of the enumerators as follows:

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 look up the enumerator of the value you pass it:

Example looking for Wednesday:

let self.dia = DiasClases(rawValue: 3)!


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: