Thanos
Thanos

Reputation: 854

Swift: How to pass closure argument from a class method to a function in the class

I have this class defined in my app to handle requests to my backend

class BackendService {      
    // Retrieves user chat channels
    class func getChatChannels(success:(response: Response)->(), failure: (response:Response)->()) {        
        let chatsURL = baseURL + "/chats"
        performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
    }

    func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?) {
        var manager = Alamofire.Manager.sharedInstance

        let keychain = Keychain(service:"com.domain.app")
        let token = keychain.get("token")

        if let token = token {            
            manager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer \(token)"]

            manager.request(.GET, url, parameters:nil).responseJSON { (req, res, json, err) in

                if(err != nil) {                    
                    var response = Response()
                    response.error = err
                    failure(response: response)                    
                } else {                    
                    var response = Response()

                    if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
                        response.httpStatus = httpStatus
                    }

                    response.payload = JSON(json!)
                    success(response: response)
                }
            }            
        }
    } 
}

I am trying to pass the callback/closure arguments that getChatChannels receives to performRequestWith. In performRequestWith(success, failure: failure, url: chatsURL, parameters: nil) I am getting Extra argument 'failure' in call

I have little experience with Swift and I am clearly doing something awfully wrong here. Some help would much appreciated.

Upvotes: 1

Views: 2067

Answers (1)

Midhun MP
Midhun MP

Reputation: 107231

There is no issue with your method calling code. The issue is you are calling a instance method from a class method.

Either you should change both methods to class method, like:

class func getChatChannels(success:(response: Response)->(), failure: (response:Response)->())
{        
    let chatsURL = baseURL + "/chats"
    performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
}

class func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?)
{
    // Your code
}

or Change both to instance method, like:

func getChatChannels(success:(response: Response)->(), failure: (response:Response)->())
{        
    let chatsURL = baseURL + "/chats"
    performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
}

func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?)
{
    // Your code
}

Another option is create an instance of the class inside the class method and using that call the instance method.

Upvotes: 1

Related Questions