I can perform the requests with Alamofire, but I saw that it is not possible to use return within the scope of success.
After a search, I saw that the solution is to use a closure pattern, to get the value of the request when it is finished, so I implemented the following code:
// Em qualquer lugar do codigo faço a chamada
func pegaNaAPI(){
var valor = realizaRequisicao("chinelo", "vassoura", completionHandler: { (result, error) -> MeuModel in
return result!
})
if valor.soma = 0 {
print("É de graça!")
}
}
=================
// Implementacao para realizar requisicao
func realizaRequisicao(paramA : String, paramB: String, completionHandler: (result:MeuModel?, error:NSError?) -> MeuModel) {
makeCall(paramA, paramB, completionHandler: completionHandler)
}
func makeCall(paramA : String, paramB: String, completionHandler: (result:MeuModel?, error:NSError?) -> MeuModel){
Alamofire.request(.GET, urlRequisicao, parameters: nil).responseJSON { response in
switch response.result {
case .Success(let data):
completionHandler(result: data as? NSDictionary, error: nil)
break
case .Failure( _):
completionHandler(result: data as? NSDictionary, error: nil)
break
}
}
}
Then I did another search, and saw that closures can perform 'return' data.
In this code snippet you have a problem:
if valor.soma == 0 {
print("É de graça!")
}
The error message I get is:
Value of tuple type '()' has no member 'sum'
How can I ensure the type of the returned value? Because soma
property is present in MeuModel