Sinan Samet
Sinan Samet

Reputation: 6752

Binary operator != cannot be applied to operands of type NSUrlConnection!.Type and NilLiteralConvertible

I'm new to Swift and I finally managed to finish my first app but when the update got released my code got totally screwed up.. The most errors in my code is caused by sendSynchronousRequest giving Extra argument 'error' in call error. I tried to change it and finally came up with this.

    var url = NSURL(string: "http://****")
    var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: Double.infinity);
    if IJReachability.isConnectedToNetwork(){
        request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: Double.infinity);
    }
    var response: NSURLResponse?
    let data = NSURLConnection!.self
    do {
        let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
    } catch (let e) {
        print(e)
    }
    if data != nil {
        var dataArray = JSON(data: data)
        let dutch_sentence = dataArray[id]["dutch_sentence"]
        let polish_sentence = dataArray[id]["polish_sentence"]
        let navigationTitle = dutch_sentence.string!.uppercaseString
        self.title = navigationTitle

        //Populate labels
        dutchSentenceLabel.text = dutch_sentence.string!
        polishSentenceLabel.text = polish_sentence.string!

    }

But now it's saying:

Binary operator != cannot be applied to operands of type NSUrlConnection!.Type and NilLiteralConvertible

For the line if data != nil {

Upvotes: 1

Views: 1346

Answers (1)

ridvankucuk
ridvankucuk

Reputation: 2407

Consider using NSURLSessionTask instead of NSUrlConnection which is deprecated in iOS9 such as:

var url = NSURL(string: "http://****")
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: Double.infinity);
    if IJReachability.isConnectedToNetwork(){
        request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: Double.infinity);
    }
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
    if data != nil {
        var dataArray = JSON(data: data)
        let dutch_sentence = dataArray[id]["dutch_sentence"]
        let polish_sentence = dataArray[id]["polish_sentence"]
        let navigationTitle = dutch_sentence.string!.uppercaseString
        self.title = navigationTitle

        //Populate labels
        dutchSentenceLabel.text = dutch_sentence.string!
        polishSentenceLabel.text = polish_sentence.string!

    }
});

Upvotes: 1

Related Questions