我正在尝试创建一个简单的HTTP框架。
首先,我有以下类型别名:

typealias successBock = ([Any]) -> ()
typealias errorBlock = (NSError) -> ()
typealias requestResponseTuple = (con: NSURLConnection, success: successBock?, error: errorBlock?)

我在这个方法中有一个错误:
func performHttpRequest<T>(method: HTTPRequestMethod, path: String?, parameter:Dictionary<String, String>,
    success:successBock?, error:errorBlock?) -> Int {
        var url = NSURL(string: baseURL.stringByAppendingPathComponent((path != nil) ? path! : ""))
        var request = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 20.0)
        var con = NSURLConnection(request: request, delegate: self, startImmediately: true)
        var requestId = newRequestId()
        currentActiveRequests[requestId] = requestResponseTuple(con, successBock.self, errorBlock.self)
        return requestId;
}

此处显示错误:
requestResponseTuple(con, success, errorBlock.self)

错误:“'successbock.type'不能转换为'successbock'”
我想通过这个街区,这样我以后就可以打电话了。在目标C中,我从来没有遇到过这样的问题。
老实说,我不知道为什么会这样。我查了几页,但没有找到解决办法。
顺祝商祺!
小精灵
编辑2:
更改了一种方法:
func performHttpRequest<T>(method: HTTPRequestMethod, path: String?, parameter:Dictionary<String, String>,
    success:successBock?, error:errorBlock?) -> Int {
        var url = NSURL(string: baseURL.stringByAppendingPathComponent((path != nil) ? path! : ""))
        var request = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 20.0)
        var con = NSURLConnection(request: request, delegate: self, startImmediately: true)
        var requestId = newRequestId()
        currentActiveRequests[requestId] = requestResponseTuple(con, success, error)
        return requestId;
}

现在出错:找不到接受所提供参数的“subscript”的重载
编辑:
代码链接:
http://pastebin.com/c5a0fn1Nhttp://pastebin.com/d0QGQ2AR

最佳答案

好吧,如果我理解的很好,你就必须改变

requestResponseTuple(con, successBock.self, errorBlock.self)

通过
requestResponseTuple(con!, success, error)

您的参数名是成功和错误。SuccessBock和ErrorBlock是类型。
所以我建议你把它们大写。
希望对你有帮助。
编辑:由于NSURLConnection返回一个可选选项,因此必须将其展开。
在打开之前,您应该检查con是否等于nil。
if let con = NSURLConnection(request: request, delegate: self, startImmediately: true)
        {
            currentActiveRequests[requestId] = RequestResponseTuple(con, success, error)
        }
        else
        {
                // there was an error, the NSURLConnection has not been created
        }

关于swift - Swift Closures作为元组中的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27923108/

10-10 21:32