Search code examples
swiftasynchronousclosuresalamofire

Syntax for a function with closure and arguments in a function on Swift


I looked around stackoverflow and tried to find what I need, but couldn't find it, if its duplicate, please point me in the right direction.

The question is, the syntax when trying to call a function that has a argument and a closure to it is confusing me. All the examples found seem to have a completion handler only, and no arguments to it.

I'm using Alamofire and want to make a wrapper class that handles some stuff that would be redundant to implement it all over the program.

What I have is

class ApiInterface{

    func getFromAPI( targetURL: String, completionHandler: (NSDictionary?, NSError?) -> ()) {

        Alamofire.request(.GET, targetURL, parameters: params)
        .responseJSON { response in
            completionHandler(response.result.value as? NSDictionary, response.result.error as? NSError)
        }
    }    
}

My idea was

ApiInterface().getFromAPI{ targetURL, (response, error) in 


}

but this causes and error and the XCode error messages don't help much.

I don't think its not possible to do this, so please help me.


Solution

  • Try this:

    ApiInterface().getFromAPI(targetURL, completionHandler: { (response, error) in
    })