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

标签 xcode swift

我是 Swift 的新手,我终于完成了我的第一个应用程序,但是当更新发布时,我的代码完全搞砸了。我代码中的大多数错误是由 sendSynchronousRequest 引起的调用中的额外参数“错误” 错误。我试图改变它,最后想到了这个。

    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

对于行 if data != nil {

最佳答案

考虑使用 NSURLSessionTask 而不是 NSUrlConnection,后者在 iOS9 中已弃用,例如:

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!

    }
});

关于xcode - 二元运算符 != 不能应用于 NSUrlConnection!.Type 和 NilLiteralConvertible 类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725325/

相关文章:

ios - 是否可以使用 xcodebuild 完全不用 Xcode 来开发 iOS 应用程序?

swift - 聚焦时向 UIButton 添加操作(快速)

ios - 错误 ITMS-90207 苹果商店提交

ios - 上面的 UITabBarItem 图像而不是文本旁边

ios - 关键观察者没有停止更新新值?

ios - 大型 UINavigationBar 在弹出时平滑地调整大小 rootViewController

ios - base64 字符串到 iOS 中的音频 mp3?

ios - 在 xcode 中启动时模拟器崩溃,问题出在 view.controller.init 中

ios - 当设备未连接时,Xcode 在 bool 上编译错误

ios - 如何保存 1 个 xcode 项目中的所有build设置并将它们用于其他 xcode 项目?