I am currently wanting to convert an object into swift of this class
class UsuarioServer{
var username:String?
var password:String?
var nombreCompleto:String?
var direccion:String?
var edad:String?
init (username: String, password: String, nombreCompleto:String,direccion:String,edad:String) {
self.username = username;
self.password = password;
self.nombreCompleto = nombreCompleto;
self.direccion = direccion;
self.edad = edad;
}
init(){}
}
and then I initialize it and set it like this:
var usuarioServidor:UsuarioServer?
usuarioServidor?.username = nombreUsuario.text!
usuarioServidor?.password = password.text!
usuarioServidor?.direccion = direccion.text!
usuarioServidor?.nombreCompleto = nombreCompleto.text!
The problem is that I need to send those parameters to an api rest as an NSOBJECT so I thought to serialize it but I do not know how to do it in swift 3 (since I'm new to swift)
some idea of how to transform that object to JSON since I am currently trying to do it this way:
var usuarioServidor:UsuarioServer?
usuarioServidor?.username = nombreUsuario.text!
usuarioServidor?.password = password.text!
usuarioServidor?.direccion = direccion.text!
usuarioServidor?.nombreCompleto = nombreCompleto.text!
do {
let jsonData = try JSONSerialization.data(withJSONObject: usuarioServidor, options: .prettyPrinted)
print(jsonData)
} catch {
//handle error
print(error)
}
but it throws an error (I guess because it's an object)