Use this tag only for questions directly pertaining to the Swift Decodable protocol introduced in Swift 4.
Questions tagged [decodable]
678 questions
6
votes
1 answer
Codable/Decodable should decode Array with Strings
Why is the names Array not decoding?
Prepared for Playground, Simple paste this into your playground
import Foundation
struct Country : Decodable {
enum CodingKeys : String, CodingKey {
case names
}
var names :…

Jan
- 12,992
- 9
- 53
- 89
5
votes
1 answer
How to make my struct Identifiable? It has a unique numeric uid field
In a simple project at Github I am trying to download a list of JSON objects:
struct TopResponse: Codable {
let data: [Top]
}
struct Top: Codable /*, Identifiable */ {
let uid: Int
let elo: Int
let given: String
let photo:…

Alexander Farber
- 21,519
- 75
- 241
- 416
5
votes
2 answers
'self.init' isn't called on all paths before returning from initializer
I'm working with CoreData and JSON parsing with Decodable following this and encountered an error at the end of the initializer saying 'self.init' isn't called on all paths before returning from initializer:
import Foundation
import…

Charlie Wang
- 97
- 1
- 6
5
votes
2 answers
Swift: Decode imprecise decimal correctly
I need to decode (Decodable protocol) an imprecise decimal value correctly, from this question I understand how to properly handle the Decimal instantiation, but how can I do this when decoding?
If trying to init any number as a String
if let value…

Jose
- 263
- 4
- 14
5
votes
2 answers
JSONDecoder can't handle null response
Context
I am working with the Firebase Database REST API and JSONDecoder / JSONEncoder. It's been working pretty well so far.
However for removing data the expected returned response is null, and JSONDecoder doesn't seem to like that very much.
This…

Nick
- 321
- 3
- 16
5
votes
1 answer
Parsing JSON using Decodable in Swift 4
Can Someone help me to solve the problem, length of Int
I am trying to get values from a JSON. After executing, I always get the error is:
Error serializing json: dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue:…

Benjamin So
- 97
- 1
- 6
5
votes
0 answers
Swift 4 JSON Value is null
I am having issues gracefully handling this error debugDescription: "Expected String value but found null instead."
I understand that in my struct that conforms to Codable, my stored property would need to be declared an optional string, as I read…

Max
- 173
- 2
- 8
4
votes
0 answers
Decode error: The given data did not contain a top-level value
I have a strange problem with decodable.
The JSON is just simple like this:
{
"slug" : "marketplace",
"taxonomy_id" : 1,
"taxonomy" : "category",
"parent_id" : 1,
"shop_id" : "raw",
"filter_id" : 1,
"term_id" : 1,
…

Jefferson Setiawan
- 536
- 1
- 5
- 22
4
votes
1 answer
Trailing closure passed to parameter of type 'Set' that does not accept a closure
Was reviewing my colleague code and this error appears.
Any ideas what's wrong with it? Was trying to Run the build and unfortunately this error appears
Error -> Trailing closure passed to parameter of type 'Set' that does not accept a…

Hexycode
- 428
- 8
- 26
4
votes
4 answers
Why do I get ‘Cannot get keyed decoding container -- found null value instead.’ error when I use JSONDecoder
(unknown context at $10741e078).CodingKeys>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil)
my model is ,
struct Details : Decodable {
var…

Faheem Rahman
- 343
- 2
- 10
4
votes
3 answers
How should I decode a json object using JSONDecoder if I am unsure of the keys
I have an api response in the following shape -
{
"textEntries":{
"summary":{
"id":"101e9136-efd9-469e-9848-132023d51fb1",
"text":"some text",
"locale":"en_GB"
},
"body":{
…

Teddy K
- 820
- 1
- 6
- 17
4
votes
3 answers
Best approach to create Non-optional Codable with `default values` in swift
I know the basic concept of class and struct but which is more effective to create models for API to fetch data and tell me with pros and cons.
Previously i don't use optional for models. Instead i give it some value. ie
class CompanyInfo :…

Wahab Khan Jadon
- 875
- 13
- 21
4
votes
1 answer
Decoding empty JSON string value into nil with Decodable
Suppose I have a struct like this:
struct Result: Decodable {
let animal: Animal?
}
And an enum like this:
enum Animal: String, Decodable {
case cat = "cat"
case dog = "dog"
}
but the JSON returned is like this:
{
"animal": ""
}
If I…

ruhm
- 1,357
- 2
- 9
- 10
4
votes
2 answers
Swift 4 decodable with unknown dynamic keys
I have the following JSON
{"DynamicKey":6410,"Meta":{"name":"","page":""}}
DynamicKey is unknown at compile time.I'm trying to find a reference how to
parse this struct using decodable.
public struct MyStruct: Decodable {
public let unknown:…

sger
- 719
- 2
- 12
- 26
4
votes
1 answer
Decodable nested data without creating additional class in Swift
I'm new in iOS development, so sorry for stupid question in advance.
I have json like this:
{
"type":"post",
"comments":{
"count":0,
"can_post":1
},
"likes":{
"count":0,
"user_likes":0,
"can_like":1,
…

Magnus Carlsen
- 43
- 3