Use this tag only for questions directly pertaining to the Swift Decodable protocol introduced in Swift 4.
Questions tagged [decodable]
678 questions
106
votes
12 answers
Codable class does not conform to protocol Decodable
Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message?
class Bookmark: Codable {
weak var publication: Publication?
var indexPath: [Int]
var locationInText = 0
enum CodingKeys: String, CodingKey {
…

Melodius
- 2,505
- 3
- 22
- 37
65
votes
1 answer
Custom Swift Encoder/Decoder for the Strings Resource Format
I have been playing around with Codable and reading and writing JSON from and to a file. Now I would like to write a custom Coder that can read and write iOS .strings files. Can anyone help me with this? I found the protocols Encoder and Decoder,…

Banana
- 4,010
- 9
- 33
- 49
57
votes
3 answers
Swift 4 Decodable with keys not known until decoding time
How does the Swift 4 Decodable protocol cope with a dictionary containing a key whose name is not known until runtime? For example:
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
…

matt
- 515,959
- 87
- 875
- 1,141
55
votes
13 answers
How to use Any in Codable Type
I'm currently working with Codable types in my project and facing an issue.
struct Person: Codable
{
var id: Any
}
id in the above code could be either a String or an Int. This is the reason id is of type Any.
I know that Any is not…

PGDev
- 23,751
- 6
- 34
- 88
48
votes
3 answers
What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?
I am using Codable protocol from Swift 4 first time, I am not able to understand use of decodeIfPresent from Decodable.
/// Decodes a value of the given type for the given key, if present.
///
/// This method returns `nil` if the container does not…

technerd
- 14,144
- 10
- 61
- 92
32
votes
3 answers
Protocol type cannot conform to protocol because only concrete types can conform to protocols
Within the app, we have two types of Stickers, String and Bitmap. Each sticker pack could contain both types. This is how I declare the models:
// Mark: - Models
protocol Sticker: Codable {
}
public struct StickerString: Sticker, Codable,…

Roi Mulia
- 5,626
- 11
- 54
- 105
28
votes
2 answers
Decoding Void with Swift 4’s Decodable
I have a generic REST request:
struct Request {…}
The T is the return type of the request, for example:
struct Animal {…}
let animalRequest = Request
let animal: Animal = sendRequest(animalRequest)
Now I would like to express that the…

zoul
- 102,279
- 44
- 260
- 354
22
votes
4 answers
Expected to decode Int but found a number instead
I had issue with JSON parsing in Swift 4.2. Here is the following code which shown runtime error.
My Json data is as follow which i got from server.
{
code: 406,
message: "Email Address already Exist.",
status: 0
}
I am using Codable…

Jatin Chauhan
- 1,107
- 1
- 8
- 21
22
votes
2 answers
Swift Codable null handling
I have a struct that parse JSON using Codable.
struct Student: Codable {
let name: String?
let amount: Double?
let adress: String?
}
Now if the amount value is coming as null the JSON parsing is failing.
So should I manually handle the…

Ekra
- 3,241
- 10
- 41
- 61
21
votes
3 answers
debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil)
I want to load an online json file into my application, but I am running into this error:
typeMismatch(Swift.Array,
Swift.DecodingError.Context(codingPath: [], debugDescription:
"Expected to decode Array but found a dictionary…

Brent Le Comte
- 255
- 1
- 2
- 7
20
votes
2 answers
Swift Codable multiple types
I try to parse an api returning a json object. My problem is that some keys are sometime a string, sometime an object like the key "Value" in the following example:
[
{
"Description": null,
"Group": "Beskrivning av enheten",
…

Sylvain
- 509
- 1
- 7
- 17
18
votes
1 answer
Swift 4 Decodable - No value associated with key CodingKeys
I'm decoding a JSON response in my Swift App, and the code used to work till it decided to stop working.
this is my json reposnse
{
"foods": [
{
"food_name": "Milk Chocolate",
"brand_name": "Snickers",
…

Stu-dying
- 193
- 1
- 1
- 5
18
votes
2 answers
Inheritance of Encodable Class
I am writing a program using Swift 4 and Xcode 9.2. I have faced difficulties with writing encodable class (exactly class, not struct). When I am trying to inherit one class from another, JSONEncoder does not take all properties from sub class…

Alex
- 1,038
- 2
- 12
- 32
16
votes
2 answers
Swift Codable init
I would like to do some initialization logic after the Swift Coding/Encoding feature has finished decoding a JSON.
struct MyStruct: Codable {
let id: Int
var name: String
init() {
name = "\(id) \(name)"
}
}
But I get the…

Darko
- 9,655
- 9
- 36
- 48
14
votes
1 answer
Swift 4 Decodable: The given data was not valid JSON
I'm trying to write a POST request to my local server, this is my function:
@IBAction func postButtonAction(_ sender: UIButton) {
guard let url = URL(string:"http://localhost:443/api/message") else {return}
var request = URLRequest(url:…

Kei
- 611
- 2
- 11
- 24