Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to cast [Any] to Dictionary

I'm using Socket.IO, the data is of type [Any] but my structure is expecting a type Dictionary but XCode is telling me that the cast always fail and it's make my app crash so there is a sample of what i'm doing :

let defaults = UserDefaults.standard
let check = defaults.integer(forKey: "id")
let jsonObject: [String: Any] = [
 "user_id1": check,
 "user_id2": My_Friends.result![y].id,
]
socket?.on("getmessages") { data, ack in
 print("DATA ========================", data)     
 let test = Hoze_Messages(json: data as! Dictionary<String,Any>)
}
socket?.emit("getmessages", jsonObject)

Then this is the data that I print :

DATA ======================== [{"data":{"status":"ok","error":200,"format":"JSON"},"result":[{"id":48,"sender_id":176,"recipient_id":171,"sender":"test123456","recipient":"Medou","text":"salut","date":"2018-12-03T20:32:20.000Z","status":0,"type":1},{"id":49,"sender_id":176,"recipient_id":171,"sender":"test123456","recipient":"Medou","text":"ça va","date":"2018-12-03T20:32:25.000Z","status":0,"type":1},{"id":50,"sender_id":176,"recipient_id":171,"sender":"test123456","recipient":"Medou","text":"123","date":"2018-12-03T20:34:27.000Z","status":0,"type":1},{"id":51,"sender_id":176,"recipient_id":171,"sender":"test123456","recipient":"Medou","text":"dedzedz","date":"2018-12-04T21:50:05.000Z","status":0,"type":1},{"id":52,"sender_id":176,"recipient_id":171,"sender":"test123456","recipient":"Medou","text":"jhhhhh","date":"2018-12-04T21:50:09.000Z","status":0,"type":1},{"id":53,"sender_id":171,"recipient_id":176,"sender":"Medou","recipient":"test123456","text":"test","date":"2018-12-08T16:59:30.000Z","status":0,"type":1},{"id":54,"sender_id":171,"recipient_id":176,"sender":"Medou","recipient":"test123456","text":"Julien","date":"2018-12-09T15:44:52.000Z","status":0,"type":1}]}]

There is both of my structure :

import Gloss

struct Hoze_Messages: JSONDecodable {
    let data: Hoze_Data?
    let result: [Hoze_MResult]?

    init?(json: JSON) {
        self.data = "data" <~~ json
        self.result = "result" <~~ json
    }
}

struct Hoze_MResult: JSONDecodable {
    let sender_id: Int?
    let recipient_id: Int?
    let sender: String?
    let recipient: String?
    let text: String?
    let status: Int?
    let type: Int?

    init?(json: JSON) {
        self.sender_id = "sender_id" <~~ json
        self.recipient_id = "recipient_id" <~~ json
        self.sender = "sender" <~~ json
        self.recipient = "recipient" <~~ json
        self.text = "text" <~~ json
        self.status = "status" <~~ json
        self.type = "type" <~~ json
    }
}

Comments