Use this tag only for questions directly pertaining to the Swift Decodable protocol introduced in Swift 4.
Questions tagged [decodable]
678 questions
13
votes
2 answers
Got this error: nw_protocol_get_quic_image_block_invoke dlopen libquic failed
I was trying to connect my API data to view it in the cell but it seems that I can't get my response and it's always == nil
The code below describes the Country.SWIFT // Model.SWIFT // Response.SWIFT which is showing how can I get my JSON response…

Menaim
- 937
- 7
- 28
13
votes
1 answer
Ignore non-Codable optional properties in Codable object
When conforming to Codable protocol, I cannot easily skip optional property of non-Codable class
In Ride struct we want to skip encoding and decoding of driver property, and just leave it nil when decoding:
struct Ride: Codable {
public var…

Igor Kovryzhkin
- 2,195
- 1
- 27
- 22
13
votes
2 answers
Swift 4 Decodable - Additional Variables
Something I havent figured out or have been able to find online as of yet.
Is there a way to add additional fields onto a struct containing the decodable protocol in which are not present in the JSON Data?
For example and simplicity, say I have an…

Rykuno
- 458
- 3
- 14
11
votes
2 answers
Swift 4 JSON decoding when type is only known at runtime
Is it possible with the Decodable protocol in Swift 4 to decode a JSON object when the type to decode to is only known at runtime?
I have a registry of sorts which maps a String identifier to the type we want to decode to, as below:
import…

Dave Rogers
- 135
- 7
10
votes
3 answers
Make a protocol Codable and store it in an array
I have the Animal protocol with 2 structs that conform to it and a Farm struct which stores a list of Animals. Then, I make them all conform to Codable to store it in a file, but it throws the error cannot automatically synthesize 'Encodable'…

ngngngn
- 579
- 4
- 18
10
votes
1 answer
Swift Init does not conform to expected type 'Decoder'
At the moment, I have a struct that conforms to Codable:
public struct Preference: Codable {
public let id: String
}
When I try to initialize the object using the following:
let preference = Preference(id: "cool")
I get the following…

Adam Cooper
- 867
- 1
- 6
- 23
10
votes
1 answer
How to reference a generic Decodable struct in Swift 4
I have a function that I would like to reuse and have it accept a parameter of a Decocable struct. For example, this is a simplification of my current code (assume "MyDecodableStruct" is a Decodable struct declared elsewhere in the app):
static…

Kate M
- 478
- 4
- 17
9
votes
1 answer
Swift 5 Default Decododable implementation with only one exception
Is there a way to keep Swift's default implementation for a Decodable class with only Decodable objects but one exception?
So for example if I have a struct/class like that:
struct MyDecodable: Decodable {
var int: Int
var string: String
…

iVentis
- 993
- 6
- 19
9
votes
3 answers
Swift: Codable - extract a single coding key
I have the following code to extract a JSON contained within a coding key:
let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"])
This successfully handles the following JSONs:
{
"applmusic":{
"code":"AAPL",
…

Richard Topchii
- 7,075
- 8
- 48
- 115
9
votes
1 answer
Swift 4 Decodable: struct from nested array
Given the following JSON document I'd like to create a struct with four properties: filmCount (Int), year (Int), category (String), and actor (Actor array).
{
"filmCount": 5,
"year": 2018,
"category": "Other",
"actors":{
…

Brychan
- 133
- 1
- 4
9
votes
7 answers
Swift Codable decode empty json as nil or empty object
Here's my code:
class LoginUserResponse : Codable {
var result: String = ""
var data: LoginUserResponseData?
var mess: [String] = []
}
public class LoginUserResponseData : Codable {
var userId = "0"
var name = ""
}
Now, calling…

Makalele
- 7,431
- 5
- 54
- 81
9
votes
3 answers
Decodable conformance with property of enum type
I have this enum:
enum DealStatus:String {
case PENDING = "Pending"
case ACTIVE = "Active"
case STOP = "Stop"
case DECLINED = "Declined"
case PAUSED = "Paused"
}
and struct:
struct ActiveDeals: Decodable {
let keyword: …

Sushil Sharma
- 2,321
- 3
- 29
- 49
8
votes
4 answers
Swift custom decodable initializer without CodingKeys
Let's say I have the following decodable struct as an example illustrating what I'm trying to do:
struct Object: Decodable {
var id: String
var name: String
}
and this JSON:
[
{
"id": "a",
"name": "test"
},
{
…

gnarlybracket
- 1,691
- 4
- 18
- 37
8
votes
4 answers
Swift Encodable, Decodable Vs Codable
Found in Apple doc, that Codable protocol is composed of Encodable and Decodable. Thus,
Codable = Encodable & Decodable
Now, let's say I implemented below classes,
class X: Codable {
var name: String
}
class Y: Encodable, Decodable {
var…

Sazzad Hissain Khan
- 37,929
- 33
- 189
- 256
8
votes
1 answer
Swift Codable not working as expected?
{
"responseBody": {
"table": {
"data": [
[
"Forth Record",
null,
0,
"2018-08-23T18:30:01.000+0000",
0,
0,
"HCL",
…

iamVishal16
- 1,780
- 18
- 40