Use this tag only for questions directly pertaining to the Swift Decodable protocol introduced in Swift 4.
Questions tagged [decodable]
678 questions
4
votes
4 answers
How to parse JSON with Decodable protocol when property types might change from Int to String?
I have to decode a JSON with a big structure and a lot of nested arrays.
I have reproduced the structure in my UserModel file, and it works, except with one property (postcode) that is in a nested array (Location) that sometimes is an Int and some…

Alfro
- 1,524
- 2
- 21
- 37
4
votes
4 answers
Swift JSON variables with all numeric names. What's the work around?
I am pulling some JSON data that I need to parse using Decodable into Swift.
As most of the data is dates I have 365 date entries along the lines of:
"20170101": 0.17,
"20170102": 1.0,
"20170103": 0.68,
"20170104": 0.61,
"20170105": 1.03,…

Edward Hasted
- 3,201
- 8
- 30
- 48
4
votes
1 answer
How to make a struct conforms to a protocol which has a property conforms to another protocol in swift 4?
I was going to reflect some JSON data from web service into swift struct. So I created a protocol which conforms to decodable protocol and planed to create some structs to conform it. This is the protocol I had created:
protocol XFNovelApiResponse:…

Rabbit Zhou
- 123
- 1
- 8
4
votes
0 answers
How do I do a partial update in Realm using `Decodable` object without data loss?
I have an JSON-API that returns me arrays of Courses. The Courses have CourseLocations, CourseTypes etc. All of them have primary keys. I thought it would be great to parse everything using Codable and to update it using…

Jarod
- 485
- 5
- 16
4
votes
3 answers
Unit testing conformance to Decodable using a SingleValueDecodingContainer
So, I have a type which looks like this:
struct Identifier {
let string: String
}
extension Identifier: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
string = try…

vrutberg
- 1,981
- 1
- 20
- 28
4
votes
1 answer
How to decode a nested JSON with many unique keys in Swift?
My questions is what is the best method I could use to decode a JSON that has many different keys? Right now, I am creating a var for every single one. This seems inefficient/hard coding, and I have about 8 different menus like this to decode.
Here…

winstonj
- 53
- 1
- 3
4
votes
0 answers
Swift JSONDecoder decode Int64 on iOS 10
There is a bug in iOS 10, that Swift 4's JSONDecoder does not decode some huge 64 bit integers from JSON with error "Parsed JSON number <1522853867156381000> does not fit in Int64."
This is not happening in iOS 11 and it is a known bug
None of…

shelll
- 3,234
- 3
- 33
- 67
4
votes
2 answers
Subclassing swift generic decodable type
EDIT: As Rob Napier wrote, the problem exists in Xcode 9.2. In Xcode 9.3 the issue is no longer relevant.
My server json responses are all packed inside data object:
{
"data": {...}
}
So I have the following generic type to parse JSON:
class…

NShiny
- 1,046
- 1
- 10
- 19
4
votes
0 answers
Swift decodable propagate userInfo
I have the following models:
struct Shop: Decodable {
let id: Int
let name: String
let products: [Product]
private enum CodingKeys: String, CodingKey {
case id
case name
case products
}
init(from…

nikmin
- 1,803
- 3
- 28
- 46
4
votes
2 answers
Decoding JSON array of different types in Swift
I'm trying to decode the following JSON Object
{
"result":[
{
"rank":12,
"user":{
"name":"bob","age":12
}
},
{
"1":[
…

TheBearF8
- 375
- 1
- 3
- 14
4
votes
2 answers
Extracting data from JSON array with swift Codable
I have a JSON response like this:
I have currently designed my decodable struct to be as follows:
struct PortfolioResponseModel: Decodable {
var dataset: Dataset
struct Dataset: Decodable {
var data: Array //I…

Mehul Parmar
- 3,599
- 3
- 26
- 42
4
votes
0 answers
JSONEncoder and PropertyListEncoder don't conform to Encoder?
I am trying to write an Encoder/Decoder pair that wraps an given Encoder/Decoder. The goal of this wrapper is to successfully handle cyclic references (which neither JSONEncoder nor PropertyListEncoder do), and delegate the actual encoding/decoding…

Alexander
- 59,041
- 12
- 98
- 151
4
votes
2 answers
Model relations based on a key in the attributes of a Swift Decodable object
Given the JSON:
[{
"name": "TV",
"room": "Living Room"
},
{
"name": "LightBulb 1",
"room": "Living Room"
}
]
struct Room: Decodable {
let name: String
let devices: [Device]
}
struct Device: Decodable…

bogen
- 9,954
- 9
- 50
- 89
4
votes
1 answer
How to write a Decodable for a JSON in Swift 4, where keys are dynamic?
I've a JSON like this.
I need to make a corresponding Decodable struct in my iOS app using Swift 4.
{
"cherry": {
"filling": "cherries and love",
"goodWithIceCream": true,
"madeBy": "my grandmother"
},
"odd":…

iranjith4
- 378
- 2
- 13
4
votes
1 answer
Decode PropertyList using Swift 4 Codable PropertyListDecoder()
Im trying to decode a plist using PropertyListDecoder() but when I get to the trying to access the keys, I get an error that says it's in the wrong format. I'm at a loss on what I'm doing wrong. Im under the assumption I can decode a Plist file the…

SirCJ
- 505
- 6
- 11