首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用类型为(data: NSUrlConnection?)的参数列表调用类型'JSON‘的初始值设定项

无法使用类型为(data: NSUrlConnection?)的参数列表调用类型'JSON‘的初始值设定项
EN

Stack Overflow用户
提问于 2015-09-23 04:10:07
回答 1查看 837关注 0票数 0

我得到了这个错误:

代码语言:javascript
复制
Cannot invoke initialiser for type 'JSON' with an argument list of type (data: NSUrlConnection?)

当我尝试执行这段代码时:

代码语言:javascript
复制
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的新手,我正在尝试修复我的代码中的错误,因为它已经更新了,但我在这方面遇到了困难,因为我没有使用该语言的经验。有谁能帮帮我吗?如果我已经把整个代码搞乱了,我也不会感到惊讶..

我还尝试了这个:

代码语言:javascript
复制
    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()

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

EN

回答 1

Stack Overflow用户

发布于 2015-09-23 04:19:47

我相信你打算用JSONObjectWithData:options:error

除非您是使用SwiftyJSON执行此操作。如果是这样的话,这里有一个详细介绍如何安装和使用它的excellent article

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32725952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档