Я пытаюсь прочитать из локального файла JSON
и заполнить мой tableView ячейками, которые нужно извлечь из Decoder
.Поскольку мое табличное представление было все еще пустым, я добавил точку останова в строке JSONDecoder.decode
, чтобы увидеть, что происходит.Я получаю эту ошибку, хотя я убедился, что мое соглашение об именах одинаково как в моих структурах, так и в файле JSON
.Есть ли что-то, чего мне здесь не хватает.
Поскольку мое соглашение об именах было одинаковым для всех файлов, сначала я не пытался добавить CodingKeys enum
в свои структуры.После нескольких неудачных пробежек я добавил это, но это кажется устаревшим.
Где я запускаю декодер:
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]
}
Мои структуры, которые будут использоваться для хранения данных при получении:
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"
}
}
и мой файл .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": ""
}
}]
}
}]
}
Это сообщение об ошибке, которое я получаю:
- debugDescription : "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\")."