For example, I need to change the value of the variable existUser which by default is initialized as false to true but when the variable is finished it being in false and only the value true is kept within the response of Alamofire
import Foundation
import Alamofire
class UserLoginModel {
let URL_PRODUCCION = "url-de-mi-servidor"
var username: String
var userpassword: String
var usercompany: String
var existUser: Bool?
init(username: String, userpassword: String, usercompany: String){
self.username = username
self.userpassword = userpassword
self.usercompany = usercompany
}
func test(){
let url = "\(URL_PRODUCCION)"
let parametros: Parameters = [
"password":"\(self.password!)",
"username":"\(self.user!)"
]
Alamofire.request(url, method: .post, parameters: parametros) .responseJSON {
response in
if let JSON = response.result.value{
self.existUser = true
}else{
self.existUser = false
}
}
}
func setExistUser(existUser: Bool){
self.existUser = existUser
}
func getExistUser(){
return self.existUser
}
}
From the controller I create an object of type UserLoginModel, then I call the method test () , then I call the method getExistUser () , but it gives me an error and says < strong> existUser is equal to nil .
Anyway, I could not assign a value from within Alamofire even with the setUserExist () method, I hope you have a clearer day now and you can help me help, thank you in advance.
ViewController
class ViewController: UIViewController {
var user: String?
var password: String?
var company: String?
var usuario: UserLoginModel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnIngresarClick(_ sender: Any) {
self.user = textFieldUserName.text!
self.password = textFieldUserPassword.text!
self.company = textFieldUserCompany.text!
self.usuario = UserLoginModel(username: self.user!, userpassword: self.password!, usercompany: self.company!)
self.usuario?.test()
if(self.usuario?.getExistUser()){
//Haga esto
}else{
//Haga esto otro
//Siempre está entrando por aquí, si 'existUser' la inicializo en 'false' siempre me devuelve 'false',
//pero sí la dejo como un 'optional' siempre me retorna 'nil'
}
}
}