简体   繁体   English

在Swift中使用JSONDecoder时,我不断收到“没有与键相关的值”错误。 有人可以解释这里出了什么问题吗?

[英]I keep getting the “No value associated with key” error when using the JSONDecoder in Swift. Can someone please explain what is going wrong here?

I am trying to read from a local JSON file and populate my tableView with cells to be retrieved from the Decoder . 我试图从本地JSON文件读取并填充我的tableView与要从Decoder检索的单元格。 Since my table view was still empty, I added a breakpoint on the JSONDecoder.decode line to see what is going on. 由于我的表视图仍然是空的,我在JSONDecoder.decode行上添加了一个断点,以查看发生了什么。 I get this error, even though I made sure that my naming convention is the same in both my structs and JSON file. 我收到此错误,即使我确保我的命名约定在我的结构和JSON文件中是相同的。 Is there something I am missing here. 这里有什么我想念的东西。

Since my naming convention was consistent across the files, at first I did not try add CodingKeys enum inside my structs. 由于我的命名约定在文件中是一致的,起初我没有尝试在我的结构中添加CodingKeys enum After a few failed runs I added that in but it feels kind of obsolete. 经过一些失败的运行后,我加入了它,但它感觉有点过时了。

Where I run the decoder: 我在哪里运行解码器:

let fileName = "settings"

...


   if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
       do {
            let data = try Data(contentsOf: url)
            let list = try JSONDecoder().decode(SettingsPayload.self, from: data)
            completion(list.sections)
       } catch {
            completion(nil)
       }
   }

fileprivate struct SettingsPayload: Decodable {
    let sections: [Section]
}

My structs that will be used to store the data when retrieved: 检索时将用于存储数据的结构:

struct Item: Decodable {
    let title: String
    let type: String
    let url: String

    private enum CodingKeys: String, CodingKey {
        case title = "title"
        case type = "type"
        case url = "url"
    }
}

struct Section: Decodable {
    let title: String
    let items: [Item]

    private enum CodingKeys: String, CodingKey {
        case title = "title"
        case items = "items"
    }
}

and my .json file: 和我的.json文件:

{
    "sections": [{
        "section": {
            "title": "Main Settings",
            "items": [{
                "item": {
                    "title": "Notifications",
                    "type": "notification",
                    "url": ""
                },
                "item": {
                    "title": "Log Out",
                    "type": "",
                    "url": ""
                }
            }]
        },
        "section": {
            "title": "Feedback",
            "items": [{
                "item": {
                    "title": "Contact Us",
                    "type": "email",
                    "url": ""
                },
                "item": {
                    "title": "Rate on App Store",
                    "type": "webView",
                    "url": "https://www.apple.com/uk/ios/app-store/"
                }
            }]
        },
        "section": {
            "title": "About",
            "items": [{
                "item": {
                    "title": "Terms of Service",
                    "type": "webView",
                    "url": ""
                },
                "item": {
                    "title": "Privacy Policy",
                    "type": "webView",
                    "url": "https://www.apple.com/uk/ios/app-store/"
                },
                "item": {
                    "title": "Version Info",
                    "type": "webView",
                    "url": ""
                }
            }]
        }
    }]
}

This is the error message I get: 这是我收到的错误消息:

     - debugDescription : "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\")."

I think the problem is that the decoder expects an Array of the item Section, but your json has an Array of dictionaries with a key "section" and an item Section in there. 我认为问题是解码器需要一个项目部分的数组,但你的json有一个字典数组,其中包含一个键“section”和一个项目Section。

Maybe try to modify your json like this: 也许尝试像这样修改你的json:

{
    "sections": [{
            "title": "Main Settings",
            "items": [ {
                    "title": "Notifications",
                    "type": "notification",
                    "url": ""
                }, {
                    "title": "Log Out",
                    "type": "",
                    "url": ""
                }]}, {
            "title": "Feedback",
            "items": [{
                    "title": "Contact Us",
                    "type": "email",
                    "url": ""
                },{
                    "title": "Rate on App Store",
                    "type": "webView",
                    "url": "https://www.apple.com/uk/ios/app-store/"
                }]}, {
            "title": "About",
            "items": [{
                    "title": "Terms of Service",
                    "type": "webView",
                    "url": ""
                }, {
                    "title": "Privacy Policy",
                    "type": "webView",
                    "url": "https://www.apple.com/uk/ios/app-store/"
                }, {
                    "title": "Version Info",
                    "type": "webView",
                    "url": ""
                }
  ]}]
}

Update 更新

JSONDecoder doesn't look for the name of your decodable struct in the JSON, it only looks for the name of the properties. JSONDecoder不会在JSON中查找可解码结构的名称,它只查找属性的名称。

暂无
暂无

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

相关问题 NSUserDefaults for Today Extension不保存数据,iOS很快。 在这里找不到我在做什么错 - NSUserDefaults for Today Extension not saving data, iOS swift. Can't figure out what I am doing wrong here 我一直在Swift中遇到这个错误。 “一条线上的连续声明必须分开';'” - I keep getting this error in Swift. “Consecutive Declarations On A Line Must Be Separated By ';'” 在Swift 4中使用新的JSONDecoder协议时,我得到“无法读取数据,因为数据格式不正确。” 错误 - When using the new JSONDecoder protocol in Swift 4 i'm getting the 'the data couldn’t be read because it isn’t in the correct format.' error 有人可以解释为什么此代码错误吗? - Can someone explain what why this code is wrong? JSONDecoder始终返回“没有与键CodingKeys关联的值” - JSONDecoder always returns “No value associated with key CodingKeys” 在 Swift 4.0 中使用 JSONDecoder 解码数据键的特定值 - Decode particular value for key of Data using JSONDecoder in Swift 4.0 试图用Swift中我称之为“生命模型”的东西来构建东西。 认为我可能做错了 - Trying to build something with what I'd call a 'living model' in Swift. Think I might be doing this wrong 有人可以解释下面的符号吗? - Can someone please explain the notation below? 有人可以解释一下Cocos2d-Swift,SpriteBuilder,Xcode和CocoaPods之间的区别吗? - Can someone please explain the differences between Cocos2d-Swift, SpriteBuilder, Xcode and CocoaPods? 迅速。 我通过api提取单元格中的图片。 一切都在建造中,但不是图片而是空的。 返回零。 请解释 - Swift. I pull out pictures in cells through api. Everything is being built, but instead of pictures it's empty. Returns nil. Explain please
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM