无法使用类型的参数列表调用类型为“JSON”的初始化器(数据:NSUrlConnection?)

时间:2015-09-22 20:10:07

标签: xcode swift

我收到此错误:

Cannot invoke initialiser for type 'JSON' with an argument list of type (data: 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);
}
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的新手,我正在尝试修复我的代码中的错误,因为它已更新,但由于我对语言没有经验,我正在努力工作..有人可以帮助我吗?如果我实际上已经搞砸了整个代码,我不会感到惊讶。

我也试过这个:

    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()

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

0 个答案:

没有答案