简体   繁体   English

JSON致命错误:展开可选值时意外发现nil

[英]JSON fatal error: unexpectedly found nil while unwrapping an Optional value

I am not too sure why I am getting this error. 我不太确定为什么会收到此错误。 Is there something I am not checking for in terms of an optional value or some sort of option that I am missing 是否有我没有检查的可选值或我缺少的某种选项的东西

func getJSON(){

    let url = NSURL(string: myURL)
    let request = NSURLRequest(url: url! as URL)
    let session = URLSession(configuration: URLSessionConfiguration.default)
    let task = session.dataTask(with: request as URLRequest) { (data,response,error) -> Void in

        if error == nil {

            let swiftyJSON = JSON(data:data!)

            let theTitle = swiftyJSON["results"].arrayValue

            for title in theTitle{

                let titles = title["title"].stringValue
                print(titles)
            }


        } else{
            print("Error")
        }
    }
    task.resume()
}

If the error is that url is nil when you force unwrap it, then that implies that on the line before it, where you create url you've passed a value in myURL that can't actually be parsed into an NSURL object, for some reason. 如果错误是当您强行打开urlurl为nil,则意味着在它之前的那一行创建url您已经在myURL中传递了一个实际上无法解析为NSURL对象的值,对于某些情况原因。

Print out myURL and see what it is. 打印出myURL并查看它是什么。 I bet it isn't well-formed. 我敢打赌,它的格式不正确。

As an aside, you shouldn't be force unwrapping anyway. 顺便说一句,无论如何您都不应该强行打开包装。 Try something like this: 尝试这样的事情:

guard let url = NSURL(string: myURL) else {
  print("Couldn't parse myURL = \(myURL)")
  return
}

let request = NSURLRequest(url: url as URL)  // <-- No need to force unwrap now.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:在展开可选值Swift时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value, Swift 致命错误:解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在展开Optional值时意外发现nil,获取json错误swift - fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift 使用JSON的Swift上的“致命错误:在展开可选值时意外发现nil” - “Fatal error: Unexpectedly found nil while unwrapping an Optional value” on Swift with JSON JSON解析并接收到:致命错误:在展开可选值时意外发现nil - JSON Parsing and recieved : fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:使用字典为有效JSON展开可选值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value using Dictionary for valid JSON 解析 JSON 数据 Swift “线程 4:致命错误:在展开可选值时意外发现 nil” - Parsing JSON Data Swift “Thread 4: Fatal error: Unexpectedly found nil while unwrapping an Optional value” 解析JSON时出现“致命错误:在展开可选值时意外发现nil” - “fatal error: unexpectedly found nil while unwrapping an Optional value” when parsing JSON 尝试将AlamoFire JSON响应映射到类并收到“致命错误:在展开可选包时意外发现nil” - Trying to map an AlamoFire JSON response to a Class and receiving “Fatal Error: Unexpectedly found nil while unwrapping optional” 如何解决此“致命错误:在解开可选值时意外发现 nil”? - How can I fix this "Fatal Error: unexpectedly found nil while unwrapping optional value"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM