Search code examples
iosswifttuplesafnetworkingafnetworking-2

Passing tuple to AFNetworkingRequestManger.GET in Swift


I have bridged AFNetworking 2.5 to work with my Swift project.

I have the following function:

internal func performAction(var httpMethod: String, var url : String, headers: Dictionary<String, String>?, params: Dictionary<String, AnyObject>?, successClosure: ((operation: AFHTTPRequestOperation, responseObject: AnyObject?) -> ())?, failureClosure: ((operation: AFHTTPRequestOperation, error: NSError) -> ())?) {
        let manager = AFHTTPRequestOperationManager()            

        let internalSuccessClosure = { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            if let succ = successClosure {
                succ(operation: operation, responseObject: responseObject)
            }
        }

        let internalFailureClosure = { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            if let fail = failureClosure {
                fail(operation: operation, error: error)
            }
        }

        var methodParams = (url, params, internalSuccessClosure, internalFailureClosure)

        if httpMethod == HTTP_METHOD_GET {
            manager.GET(methodParams)
        } else if httpMethod == HTTP_METHOD_POST {
            manager.POST(methodParams)
        }
    }

The Swift compiler is complaining: Missing argument for parameter 'parameters' in call for both manager.GET(methodParams) and manager.POST(methodParams)

The following call to manager.GET() compiles as expected:

manager.GET(url,
            parameters: params,
            success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
                    if let succ = successClosure {
                        succ(operation: operation, responseObject: responseObject)
                    }
                },
            failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
                    if let fail = failureClosure {
                        fail(operation: operation, error: error)
                    }
            })

I've tried basic examples of passing a tuple for multiple argument parameters, and it has worked, so I am unsure why I am having issues here.

I tried this in Playground, and it works as expected:

func addTwoNumbers(x: Int, y: Int) -> Int {
    return x + y
}

let twoNumbers = (1,2)

addTwoNumbers(twoNumbers)

I have checked out: https://medium.com/swift-programming/facets-of-swift-part-4-functions-3cce9d9bba4 and How to append a tuple to an array object in Swift code? but they haven't been able to help me solve the problem.


Solution

  • It seems that it is not possible to pass a tuple as parameter values to objective-c code.