Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode Swift NSURLCONNECTION解析json时出现致命错误_Xcode_Swift - Fatal编程技术网

Xcode Swift NSURLCONNECTION解析json时出现致命错误

Xcode Swift NSURLCONNECTION解析json时出现致命错误,xcode,swift,Xcode,Swift,我正在尝试使用以下代码访问api。 我得到了错误致命错误:当我试图解析json时,在展开可选值时意外发现nil 我不知道为什么会发生这种错误。 数据不是零 var urlFull = NSURL(string: url)! var urlrequest = NSURLRequest(URL: urlFull) let queue:NSOperationQueue = NSOperationQueue() NSURLConnection.sendAsynchronousR

我正在尝试使用以下代码访问api。 我得到了错误致命错误:当我试图解析json时,在展开可选值时意外发现nil

我不知道为什么会发生这种错误。 数据不是零

var urlFull = NSURL(string: url)!
    var urlrequest = NSURLRequest(URL: urlFull)
    let queue:NSOperationQueue = NSOperationQueue()

    NSURLConnection.sendAsynchronousRequest(urlrequest, queue: queue, completionHandler: {
        (response, data, error) -> Void in
        println(response)
        println(data)
        println(error)

        if let anError = error {
            println(error)
        } else {
            var jsonError: NSError? = nil
            let post = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as NSDictionary
            if let aJSONError = jsonError {
                println("Error parsing")
            } else {
                println("The post is: " + post.description)
            }


        }
    })

问题在于您的强制强制强制转换:作为NSDictionary。任何被退回的东西都不能放入词典

您应该始终使用可选铸造作为?在解析JSON时,如果让…可以选择展开:

let post = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as? NSDictionary

if let post = post {
    // it worked! parse post
} else {
    // it's not a dictionary.
    println(post)
    // see what you have and debug from there
}

请你把出错的代码行标出来好吗。Thankson line let post=NSJSONSerialization.JSONObjectWithDatadata,选项:nil,错误:&jsonError作为NSDictionary