codable Questions

2

I am working on mix and match iOS source code. I have implemented codable for swift data model class which reduces the burden of writing parser logic. I tried to conform my objective c class to cod...
Dric asked 31/5, 2018 at 5:55

1

I am wondering if there is a feasible way to share common properties across multiple models by using a custom decoder initializer and/or multiple Containers and Codingkeys. Here is the JSON Object ...
Covenant asked 19/2, 2021 at 18:29

3

Solved

I have 2 protocols, Filters and Parameters, both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var fi...
Lucienlucienne asked 15/5, 2018 at 8:54

2

Solved

How do you write a JSON object encoded via Swift 4 Codable protocol to a file? Before Swift 4 I used JSONSerialization.writeJSONObject but JSONSerialization.isValidJSONObject now returns false on t...
Ml asked 16/10, 2017 at 5:34

4

Is it possible to configure the Swift JSON encoder to use the sort order of the struct properties as the sort order of the dictionary keys in the JSON output? By default it uses some arbitrary (but...
Formulaic asked 1/6, 2018 at 9:58

1

Solved

I have this json string: let json = """ { "name": "Wendy Carlos", "hexA": "7AE147AF", "hexB": "851EB851" } "&quo...
Jackleg asked 15/12, 2020 at 22:33

3

Imagine a data structure as follows, containing a value in contents that is an already encoded JSON fragment. let partial = """ { "foo": "Foo", "bar": 1 } """ struct Document { let contents: Str...
Radicle asked 30/9, 2019 at 21:52

3

Solved

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. /// //...
Susurrus asked 19/9, 2017 at 5:14

3

Solved

Can someone tell me what I'm doing wrong? I've looked at all the questions on here like from here How to decode a nested JSON struct with Swift Decodable protocol? and I've found one that seems exa...
Combo asked 16/9, 2017 at 5:35

3

Solved

I have an object from 20 fields. When I get the json from the server I get an error about the decode of the json. There is a way to quickly find out which field is problematic instead of deleting ...
Charteris asked 22/1, 2020 at 7:55

4

Solved

I am attempting to decode a JSON response from a third-party API which contains nested/child JSON that has been base64 encoded. Contrived Example JSON { "id": 1234, "attributes&quo...
Thereon asked 28/9, 2020 at 22:13

15

Solved

I have a struct that implements Swift 4’s Codable. Is there a simple built-in way to encode that struct into a dictionary? let struct = Foo(a: 1, b: 2) let dict = something(struct) // now dict is ...
Puss asked 20/7, 2017 at 8:45

1

When using a decoder in a nested Codable struct, is there any way to access a property of a parent struct? The only way I can think of that might work (haven't tested yet) is to use a manual decod...
Loferski asked 7/6, 2020 at 7:47

2

Solved

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, JSONEncod...
Haunted asked 13/1, 2018 at 8:28

3

Solved

I would like to be able to save a Custom-struct to UserDefaults but for that I need it to be Codable.. I tried it like this: struct Wishlist: Codable { var name: String var image: UIImage var wi...
Nigrosine asked 1/5, 2020 at 18:3

2

I’m using Swift to try and decode JSON:API-formatted JSON results. The JSON I’m trying to parse has a shape like this: { "data": { "type": "video", "id": ...
Jadda asked 14/7, 2020 at 15:28

3

Solved

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 { } pu...
Snakebite asked 14/7, 2019 at 10:17

4

Solved

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 alread...
Swarthy asked 27/10, 2018 at 10:2

3

Solved

I have the following code: import Foundation let jsonData = """ [ {"firstname": "Tom", "lastname": "Smith", "age": "28"}, {"firstname": "Bob", "lastname": "Smith"} ] """.data(using: .utf8)! st...
Lowery asked 13/10, 2017 at 10:57

6

Solved

I'm using Swift 4's JSONEncoder. I have a Codable struct with an optional property, and I'd like this property to show up as null value in the produced JSON data when the value is nil. However, JSO...
Litta asked 13/11, 2017 at 14:38

3

Solved

I have a problem decoding a JSON structure which I cannot change to make it easier to decode (it's coming from firebase).. How do I decode the following JSON into objects? The problem is how to co...
Rosewater asked 10/1, 2019 at 13:23

1

Solved

I am trying to create a basic to-do application for command-line in Swift. Below is my function to add a new item to the to-do array, but the new entry keeps overwriting the old one, instead of cre...
Preconception asked 18/3, 2020 at 19:4

4

Solved

I am replacing my old JSON parsing code with Swift's Codable and am running into a bit of a snag. I guess it isn't as much a Codable question as it is a DateFormatter question. Start with a struct...
Annoying asked 27/9, 2017 at 23:2

3

Compiles fine with no problems: class User: Codable { let name: String let email: String } However, if we have a property not represented by a CodingKey, it demands an initializer: class User...
Franklynfrankness asked 20/2, 2020 at 23:18

1

I tried to use custom date strategy in json decoding using Codable as describe in https://mcmap.net/q/115683/-how-can-i-parse-create-a-date-time-stamp-formatted-with-fractional-seconds-utc-timezone...
Gabrielgabriela asked 9/2, 2020 at 13:11

© 2022 - 2025 — McMap. All rights reserved.