Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - downcast NSURLResponse to NSHTTPURLResponse in order to get response code

Tags:

ios

swift

I'm building rest queries in SWIFT using NSURLRequest

var request : NSURLRequest = NSURLRequest(URL: url)

var connection : NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()

My question is how to do i get response code out of the response that is returned:

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
//...
}

According to Apple: NSHTTPURLResponse which is a subclass of NSURLResponse has a status code but I'm not sure how to downcast my response object so i can see the response code.

This doesn't seem to cut it:

println((NSHTTPURLResponse)response.statusCode)

Thanks

like image 732
Jeef Avatar asked Oct 29 '14 15:10

Jeef


People also ask

What is nsurlsession in Swift?

Learn NSURLSession using Swift Part 1 - HTTP/HTTPS GET “In iOS7 and later or OS X v10.9 and later, NSURLSession is the preferred API for new code that performs URL requests” — Apple Usually, when an iOS developers wants to retrieve contents from certain URL, they choose to use NSURLConnection.

What is the nshttpurlresponse class?

The NSHTTPURLResponse class is a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses. Whenever you make HTTP URL load requests, any response objects you get back from the NSURLSession, NSURLConnection, or NSURLDownload class are instances of the NSHTTPURLResponse class.

What is a nsurlresponse object?

The metadata associated with the response to a URL load request, independent of protocol and URL scheme. The related NSHTTPURLResponse class is a commonly used subclass of NSURLResponse whose objects represent a response to an HTTP URL load request and store additional protocol-specific information such as the response headers.

What is nsurlsession in iOS 7 and later?

In iOS 7 and later, Apple suggest to use NSURLSession instead, NSURLSession is just… Swift Programming Sign in Swift Programming Sam Wang Follow Nov 3, 2014·3min read Learn NSURLSession using Swift Part 1 - HTTP/HTTPS GET “In iOS7 and later or OS X v10.9 and later, NSURLSession is the preferred API for new code that performs URL requests” — Apple


1 Answers

Use an optional cast (as?) with optional binding (if let):

func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    if let httpResponse = response as? NSHTTPURLResponse {
        println(httpResponse.statusCode)
    } else {
        assertionFailure("unexpected response")
    }
}

or as a one-liner

let statusCode = (response as? NSHTTPURLResponse)?.statusCode ?? -1

where the status code would be set to -1 if the response is not an HTTP response (which should not happen for an HTTP request).

like image 174
Martin R Avatar answered Sep 20 '22 17:09

Martin R