0

I'm trying to write a NSURLSessionDataTask in Swift, and I'm not sure how to, I have this Objective C method, that is doing pretty much exactly what I'm wanting to do in Swift.

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
     if (httpResponse.statusCode != 200) return;


     NSDictionary *responseDict = [XMLReader dictionaryForXMLData:data options:XMLReaderOptionsProcessNamespaces error:&error];
     NSDictionary *body = responseDict[@"Envelope"][@"Body"];

     dispatch_async(dispatch_get_main_queue(), ^{
     if (block) block(body, nil);
     });
}];

I've tried and tried, and I can not figure it out! I hope the community can. This is what my swift code is right now for this section. :)

var session: NSURLSession = NSURLSession.sharedSession()
var task: NSURLSessionDataTask = session.dataTaskWithURL(request, completionHandler: { (<#NSData!#>, <#NSURLResponse!#>, <#NSError!#>) -> Void in

        var httpResponse: NSHTTPURLResponse
        httpResponse = NSHTTPURLResponse(response: NSHTTPURLResponse)

})

Any ideas? An explanation would be nice, I've tried to read this in the documentation but it's kind of confusing. Also. if this is a double post, please link me! I've tried looking, but couldn't find anything close.

douglas bumby
  • 324
  • 1
  • 4
  • 13
  • You just need do replace all <#XXXX#> with variable names or `_` if you don't care about their values. – HAS Nov 05 '14 at 07:09
  • @HAS I tried just following http://stackoverflow.com/questions/24040893/how-to-use-nsurlsessiondatatask-in-swift and I believe that that is on my way to fixing my problem. I am rewriting a small library now before I can continue. Thanks for the help though. – douglas bumby Nov 05 '14 at 08:04
  • For the downcast from NSURLResponse to NSHTTPURLResponse, this might help: http://stackoverflow.com/questions/26634539/swift-upcast-nsurlresponse-to-nshttpurlresponse-in-order-to-get-response-code – Martin R Nov 05 '14 at 08:20

0 Answers0