O AnyObject rápido não é conversível em String / Int
Eu quero analisar um JSON para objeto, mas não tenho idéia de como converter AnyObject para String ou Int, pois estou recebendo:
0x106bf1d07: leaq 0x33130(%rip), %rax ; "Swift dynamic cast failure"
Ao usar, por exemplo:
self.id = reminderJSON["id"] as Int
Eu tenho a classe ResponseParser e dentro dela (responseReminders é uma matriz de AnyObjects, do AFNetworking responseObject):
for reminder in responseReminders {
let newReminder = Reminder(reminderJSON: reminder)
...
}
Então, na classe Reminder, estou inicializando assim (lembrete como AnyObject, mas é Dictionary (String, AnyObject)):
var id: Int
var receiver: String
init(reminderJSON: AnyObject) {
self.id = reminderJSON["id"] as Int
self.receiver = reminderJSON["send_reminder_to"] as String
}
println(reminderJSON["id"])
O resultado é: Opcional (3065522)
Como posso fazer o downcast de AnyObject para String ou Int, neste caso?
//EDITAR
Depois de algumas tentativas, venho com esta solução:
if let id: AnyObject = reminderJSON["id"] {
self.id = Int(id as NSNumber)
}
para Int e
if let tempReceiver: AnyObject = reminderJSON["send_reminder_to"] {
self.id = "\(tempReceiver)"
}
para corda