Does anyone knows how to solve this bug? By the way, I'm a beginner, be kind!
AF.request(URL_LOGIN, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
if response.result.erro == nil {
if let json = response.result.erro as? Dictionary<String, Any> {
if let email = json["user"] as? String {
self.userEmail = email
}
if let token = json["token"] as? String {
self.authToken = token
}
}
self.isLoggedIn = true
completion(true)
} else {
completion(false)
debugPrint(response.result.error as Any)
First of all check your spelling, erro
is pointless.
According to the error the value for result
is Result<Any, AFError>
, Result
is an enum with associated types and two cases: success
and failure
The syntax must be something like
AF.request(URL_LOGIN, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
switch response.result {
case .success(let result):
if let json = result as? Dictionary<String, Any> {
if let email = json["user"] as? String {
self.userEmail = email
}
if let token = json["token"] as? String {
self.authToken = token
}
}
self.isLoggedIn = true
completion(true)
case .failure(let error):
completion(false)
debugPrint(error)
}
}
The logic is not very logic. I'm sure that self.isLoggedIn
is not supposed to be true
if email
and token
are invalid and should be set to false
when an error occurs.