错误域=NSCocoaErrorDomain 代码=3840 “无值".UserInfo={NSDebugDescription=无值.} [英] Error Domain=NSCocoaErrorDomain Code=3840 “No value.” UserInfo={NSDebugDescription=No value.}

查看:254
本文介绍了错误域=NSCocoaErrorDomain 代码=3840 “无值".UserInfo={NSDebugDescription=无值.}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 JSON.我在从 URL 解析 JSON 时遇到问题.错误是Error Domain=NSCocoaErrorDomain Code=3840No value." UserInfo={NSDebugDescription=No value.}"

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 有效 - 我已经检查过了.这是 JSON 的结构:

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}]

这是我的代码:导入 UIKit

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: {})
}

}

推荐答案

您需要实际使用文件中的数据:

You need to actually use the data from the file:

<代码>守卫让路径 = NSBundle.mainBundle().pathForResource("sections", ofType: "json"),让 jsonData = NSData(contentsOfFile: path) else {致命错误(无法在主包中找到或加载部分文件")}做 {如果让部分 = 尝试 NSJSONSerialization.JSONObjectWithData(jsonData,选项:.MutableContainers)作为?[字符串:AnyObject] {//处理部分}...

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 } ...

这篇关于错误域=NSCocoaErrorDomain 代码=3840 “无值".UserInfo={NSDebugDescription=无值.}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆