Hi all guys. I have a comparison of two attributes (string?) with their respective entered texts. The attributes are as follows
class Usuario: NSObject {
var id:Int = Int()
var idPrivilegio:Int = Int()
var correo:String?
var clave:String?
func buscar(incluirInactivo: Bool)-> NSMutableArray
{
conexion.database!.open()
var stringConsulta = "select * from USUARIO"
let consulta: FMResultSet! = conexion.database!.executeQuery(stringConsulta, withArgumentsIn: nil)
let listaUsuarios: NSMutableArray = NSMutableArray()
if (consulta != nil)
{
while consulta.next(){
let itemUsuario: Usuario = Usuario()
itemUsuario.id = Int(consulta.int(forColumn: "CODIGOUSUARIO"))
itemUsuario.idPrivilegio = Int(consulta.int(forColumn: "CODIGOPRIVILEGIO"))
itemUsuario.correo = consulta.string(forColumn: "CORREOUSUARIO")
itemUsuario.clave = consulta.string(forColumn: "CLAVEUSUARIO")
listaUsuarios.add(itemUsuario)
}
}
conexion.database!.close()
return listaUsuarios
}
func insertar(itemUsuario: Usuario) ->Bool
{
conexion.database!.open()
let consulta: Bool = conexion.database!.executeUpdate("insert into USUARIO (CODIGOPRIVILEGIO,CORREOUSUARIO,CLAVEUSUARIO) values (\(itemUsuario.idPrivilegio),'\(String(describing: itemUsuario.correo))','\(String(describing: itemUsuario.clave))'", withArgumentsIn: nil)
conexion.database!.close()
let aux = !consulta ? false : true
return aux
}
}
The comparison is made with this line
if(usuario.correo! == txtCorreo.text && usuario.clave! == txtPassword.text){
//codigo
}
But it does not generate the comparison properly, in the debug process the variables are shown as follows
The comparison always gives me false. Please help with this inconvenience
In your insert function is where you are having the problem. Since
usuario.correo
andusuario.clave
are optional , with the methodYou are causing the database to insert the value
'Optional("una clave")'
literally, with the stringOptional
and everything, and that is why it does not match the value you enter, because they are really 2 different strings.Try using the operator
??
so that if there is a value inclave
ycorreo
, it forces theunwrap
to save it to the database, and if it doesn't, it assigns a default value (in the example, an empty string):I imagine that you will have to delete the users that you already have saved with this method, to insert the records correctly.