user1199624
user1199624

Reputation:

Swift Closures as field in Tuple

I'm trying to create a Simple HTTP Framework.

At first I have the following typealias:

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

And I have an error in this method:

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

The error goes here:

requestResponseTuple(con, success, errorBlock.self)

Error: "'successBock.Type' is not convertible to 'successBock'"

I want to pass the block, so that i'm able to call it later. In objective-c i never had a problem like that.

To be honest, i have no idea why this occurs. I checked several pages, but didn't found a solution.

Best regards,

Maik


edit2: changed one method:

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

Error now: Could not find an overload for 'subscript' that accepts the supplied arguments

Edit:

CodeLink: http://pastebin.com/c5a0fn1N http://pastebin.com/d0QGQ2AR

Upvotes: 2

Views: 1088

Answers (1)

LastMove
LastMove

Reputation: 2482

Ok if i understand well you just have to change

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

by

requestResponseTuple(con!, success, error)

Your parameter Names are success and error. successBock and errorBlock are the types. So i Suggest you to capitalize them. Hope it helps you.

edit: Since NSURLConnection returns an optional, you have to unwrap it. You should check if con is equal to nil before unwrapping it.

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
        }

Upvotes: 3

Related Questions