Error Domain=NSCocoaErrorDomain Code=3840 “No value.” UserInfo={NSDebugDescription=No value.}

北慕城南 提交于 2019-12-11 10:49:15

问题


I'm learning JSON. I have problem with parsing JSON from URL. The error is "Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}"

JSON is valid - I've checked. This is structure of JSON:

[{"id":33,"name":"5","sort":2},{"id":34,"name":"6","sort":3},{"id":35,"name":"7","sortOrder":4}]

And this is my code: import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    let urlAsString = "http://www.url.address/"
    let url = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()
    let username = "admin"
    let password = "admin"

    struct Address {

        let name: String?
        let id: String?
        let sortOrder: String?

        init(dict: [String: AnyObject]) {

            name = dict["name"] as? String
            id = dict["id"] as? String
            sortOrder = dict["sort"] as? String
        }
    }
    let path = NSBundle.mainBundle().pathForResource("sections", ofType: "json")
    let jsonData = NSData()

    do {
        if let jsonDic = try NSJSONSerialization.JSONObjectWithData(jsonData,
            options: .MutableContainers) as? [String: AnyObject] {

                let addr = Address(dict: jsonDic)                    
                print("jsonDic")
        }

        } catch let error as NSError {
            print(error)
        }
}
func backButton(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: {})
}

}


回答1:


You need to actually use the data from the file:

guard let path = NSBundle.mainBundle().pathForResource("sections", ofType: "json"), let jsonData = NSData(contentsOfFile: path) else { fatalError("Cannot find or load the sections file in the main bundle") } do { if let sections = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) as? [String: AnyObject] { // work with sections } ...



来源:https://stackoverflow.com/questions/36462298/error-domain-nscocoaerrordomain-code-3840-no-value-userinfo-nsdebugdescripti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!