无法使用类型为参数列表的“JSON"类型调用初始化程序(数据:NSUrlConnection?) [英] Cannot invoke initialiser for type 'JSON' with an argument list of type (data: NSUrlConnection?)

查看:34
本文介绍了无法使用类型为参数列表的“JSON"类型调用初始化程序(数据:NSUrlConnection?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

Cannot invoke initialiser for type 'JSON' with an argument list of type (data: NSUrlConnection?)

当我尝试执行此代码时:

When I try to execute this code:

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?
do {
    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!

}

我是 Swift 的新手,我正在尝试修复我的代码中的错误,因为它已更新,但由于我对这门语言没有经验,所以我很难过.有人可以帮我吗?如果我实际上已经搞砸了整个代码,我不会感到惊讶..

I am new to Swift and I am trying to fix the errors out of my code since it is updated but I am having a hard time on it since I am not experienced with the language.. can someone please help me out? I wouldn't be surprised if I messed up the whole code already actually..

我也试过这个:

    let 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 task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
        var dataArray = JSON(data: data!)
        let dutch_sentence = dataArray[self.id]["dutch_sentence"]
        let polish_sentence = dataArray[self.id]["polish_sentence"]
        let navigationTitle = dutch_sentence.string!.uppercaseString
        self.title = navigationTitle

        //Populate labels
        self.dutchSentenceLabel.text = dutch_sentence.string!
        self.polishSentenceLabel.text = polish_sentence.string!
    }
    task.resume()

它没有错误,但我不知道它是否是正确的方法以及它是否有效.我必须在很多地方更换它,所以在我这样做之前我想确定它是否有效.

It has no errors but I don't know if it is the correct way and if it will work. I have to replace it on a lot of places so I want to be sure about if it works before I do that.

推荐答案

我相信您打算使用 JSONObjectWithData:options:error

除非您使用 SwiftyJSON 执行此操作.如果是这样,这里有一篇优秀文章,详细介绍了如何安装并使用它.

Unless you were doing this using SwiftyJSON. If so, here is an excellent article detailing how to install and use it.

这篇关于无法使用类型为参数列表的“JSON"类型调用初始化程序(数据:NSUrlConnection?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆