在 Swift 中将元组传递给 AFNetworkingRequestManger.GET

Passing tuple to AFNetworkingRequestManger.GET in Swift

我已桥接 AFNetworking 2.5 以与我的 Swift 项目一起工作。

我有以下功能:

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)
        }
    }

Swift 编译器抱怨:Missing argument for parameter 'parameters' in call 对于 manager.GET(methodParams)manager.POST(methodParams)

以下对 manager.GET() 的调用按预期编译:

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)
                    }
            })

我已经尝试了为多个参数参数传递元组的基本示例,并且成功了,所以我不确定为什么我会在这里遇到问题。

我在 Playground 中试过了,它按预期工作:

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

let twoNumbers = (1,2)

addTwoNumbers(twoNumbers)

我已经检查过: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?但是他们没能帮我解决问题。

似乎无法将元组作为参数值传递给 objective-c 代码。