二进制运算符!=不能应用于NSUrlConnection类型的操作数!.Type和NilLiteralConvertible

时间:2015-09-22 19:31:12

标签: xcode swift

我是Swift的新手,我终于设法完成了我的第一个应用程序,但是当更新发布时,我的代码完全搞砸了......我的代码中的大多数错误都是由sendSynchronousRequest给出{{1错误。我试图改变它,最后想出了这个。

Extra argument 'error' in call

但现在它说:

    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!

    }

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

1 个答案:

答案 0 :(得分:1)

考虑使用NSURLSessionTask而不是iOS9中不推荐使用的NSUrlConnection,例如:

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!

    }
});