swift - Saving Alamofire request data into a variable -
i'm having troubles getting password salt vm server via alamofire. i'm making request server , should return me salt, can salt password, hash , send server.
the problem not understand how save salt, alamofire receives, variable, can add password , hash that:
let salted_password = user_password + salt let hash = salted_password.sha1()
where user_password
user entered password field , salt
got alamofire salt request.
here code:
func getsalt(completionhandler: @escaping (dataresponse<string>, error?) -> void) { alamofire.request("http://192.168.0.201/salt", method: .post, parameters: salt_parameters).responsestring { response in switch response.result { case .success(let value): completionhandler(response dataresponse<string>, nil) case .failure(let error): completionhandler("failure", error) } } } let salt = getsalt { response, responseerror in return response.result.value! }
it gives me following error:
binary operator '+' cannot applied operands of type 'string' , '()'.
so possible save request value variable? should do?
thank attention.
the problem here because of how implemented completion block
for example:
func someasynchronouscall(a: int, b: int, @escaping block: (_ result: int) -> void) { ... code here ... { // let's async call done , code called after call done block(a + b) } }
to use code this:
var answer: int = 0 someasynchronouscall(100, b: 200) { result in // `result` you're returning user since api calls need called asynchronously rather creating function has default return type. answer = result print(answer) } print(answer)
the print this
0 300
since declared answer 0
printed first since async call wasn't done yet, after async call done (usually few milliseconds after) printed 300
so in code should this
var salt: string? getsalt { response, responseerror in salt = response.result.value }
Comments
Post a Comment