Compare optional string with EditText swift3

0

Hello with all guys. I have a comparison of two attributes (string?) With their respective entered texts. The attributes are the following

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 I make with this line

if(usuario.correo! == txtCorreo.text && usuario.clave! == txtPassword.text){
//codigo
}

But it does not generate the comparison in an appropriate way, in the debug process the variables are shown as follows

The comparison always gives me false. Please help with this inconvenience

    
asked by Kevtho 14.07.2017 в 23:38
source

1 answer

1

In your insert function is where you are having the problem. Given that usuario.correo and usuario.clave are optional , with the method

'\(String(describing: itemUsuario.correo))'

you are causing the BBDD to insert the value 'Optional("una clave")' literally, with the string Optional and all, and that's 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 in clave and correo , force the unwrap to save it in the bbdd, and if it does not exist, assign a default value ( in the example, an empty string):

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
}

I imagine that you will have to delete the users that you already have saved with this method, to insert the records well.

    
answered by 15.07.2017 / 14:17
source