I'm new in swift and I've been probably for more than an hour around this.
I make a request to a webservice, and now i want to act according to the response code (200 Ok) or other but I can't understand the syntax for returning a value and throwing the exception.
typealias ThrowableCallBack = () throws -> Bool
func authenticate()
{
let services = ServiceManager()
do {
try services.login(username: emailField.text!, password: passwordField.text!, authenticated: { auth in
self.loadingDialog.dismiss(animated: false, completion: {
if (auth) // '() throws -> Bool' is not convertible to 'Bool'
{
self.performSegue(withIdentifier: "LoginSegue", sender: self)
}
})
} )
}
catch RequestError.invalidRequest {
showLoginFailedAlert()
}
catch {
showLoginFailedAlert()
}
}
Then on services
func login(username : String, password : String, authenticated: @escaping (_ inner: ThrowableCallBack) -> Void )
{
let parameters = [
"_username" : username,
"_password" : password
]
let request = makePostCall(request: "login", parameters: parameters, completion: {
response in
let statusCode = String(describing: response["statusCode"]!)
if (statusCode != "200")
{
authenticated( { throw RequestError.invalidRequest })
}
else
{
self.jwt = String(describing: response["jwt"]!)
authenticated( { return true })
}
} )
}
How should I fix the auth '() throws -> Bool' is not convertible to 'Bool' to be able to both catch the error or succeed ? Is my alias correct?
Thank you in advance