codable Questions

4

I am trying to map my data to Model. Where I am using Firestore snapshot listener, to get data. here, I am getting data and mapping to "User" model that; do{ let user = try User(dictionary: temp...
Gluconeogenesis asked 18/12, 2019 at 10:38

1

How can I convert Codable struct to a JSON Data object and keep the order of the struct variables? I know that Dictionary is not ordered and that JSONEncoder and JSONSerialization.data accepts only...
Moderato asked 10/11, 2020 at 20:1

4

Solved

In trying to serialize an array of float3 objects with the basic JSONEncoder, it's revealed that float3 does not conform to the Codable protocol, so this cannot be done. I tried to write a basic e...
Folberth asked 29/1, 2018 at 1:45

6

Say I have the following code: import Foundation let jsonData = """ [ {"firstname": "Tom", "lastname": "Smith", "age": {"realage": "28"}}, {"firstname": "Bob", "lastname": "Smith", "age": {"fak...
Flashing asked 13/10, 2017 at 6:17

3

Solved

I'm trying to implement Codable for a class which contains a NSAttributedString, but I get errors at compile time: try container.encode(str, forKey: .str) error ambiguous reference to member '...
Vertical asked 16/3, 2018 at 4:46

4

Solved

I have defined a enum like this: enum ClubLevel: Int, Codable { case golden = 1, silver, bronze } in my struct I have an optional property of type ClubLevel and when I decode this property in i...
Fondea asked 20/12, 2017 at 12:31

12

Solved

I have defined an enum as follows: enum Type: String, Codable { case text = "text" case image = "image" case document = "document" case profile = "profile" case sign = "sign" case inputDate ...
Punjabi asked 6/4, 2018 at 15:6

3

Solved

I'm starting to rewrite an application and I want to use Swift 4 Codable protocol to automatically convert json string to Objects and Structs. Sometimes, specially at the beginning of coding, I en...
Pelota asked 27/9, 2018 at 14:33

6

Solved

I have an API that will sometimes return a specific key value (in this case id) in the JSON as an Int and other times it will return that same key value as a String. How do I use codable to parse t...
Mckale asked 22/12, 2017 at 4:4

5

Solved

So I have an API route that returns a JSON array of objects. For example: [ {"firstname": "Tom", "lastname": "Smith", "age": 31}, {"firstname": "Bob", "lastname": "Smith", "age": 28} ] I'm try...
Zoophobia asked 29/8, 2017 at 0:7

8

Solved

Here is my JSON { "id": 1, "user": { "user_name": "Tester", "real_info": { "full_name":"Jon Doe" } }, "reviews_count": [ { "count": 4 } ] } Here is the structure I want it saved to (...
Bethune asked 14/6, 2017 at 15:47

4

Solved

I have the following Swift structs struct Session: Encodable { let sessionId: String } struct Person: Encodable { let name: String let age: Int } let person = Person(name: "Jan", age: 36) let ...
Frymire asked 25/10, 2018 at 7:55

6

Solved

My data structure has an enum as a key, I would expect the below to decode automatically. Is this a bug or some configuration issue? import Foundation enum AnEnum: String, Codable { case enumVal...
Coahuila asked 23/6, 2017 at 15:34

3

Solved

private func createWeatherObjectWith(json: Data, x:Any.Type ,completion: @escaping (_ data: Any?, _ error: Error?) -> Void) { do { let decoder = JSONDecoder() decoder.keyDecodingStrategy = ....
Inpour asked 19/11, 2018 at 2:22

3

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 erro...
Diatribe asked 6/5, 2018 at 23:19

13

Solved

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...
Galitea asked 17/1, 2018 at 9:7

8

Solved

Swift 4 added the new Codable protocol. When I use JSONDecoder it seems to require all the non-optional properties of my Codable class to have keys in the JSON or it throws an error. Making every ...
Annalee asked 15/6, 2017 at 19:18

4

Solved

I want to encode an optional field with Swift's JSONEncoderusing a struct that conforms to the Encodable protocol. The default setting is that JSONEncoder uses the encodeIfPresent method, which me...
Insomnolence asked 14/11, 2017 at 16:22

6

I have a Codable class: class Task: Codable { var name: String } When I try to instantiate it: let newTask = Task() allTasks.append(newTask) It gives me error: Missing argument for parame...
Diplococcus asked 22/1, 2019 at 5:40

5

I'm using Codable for my WebRequest response which is returning some predefined string or number. So, I'm using Enums for those. But when some unexpected value arrive to Response at that my Codable...
Met asked 17/6, 2021 at 14:7

4

Solved

I have an EmailVerificationStatus enum with an associated type of String that conforms to the Codable protocol: enum EmailVerificationStatus: String, Codable { case unverified case verified } ...
Escrow asked 3/9, 2017 at 14:1

3

Solved

I want to upload an image file to the backend server, using certain URL endpoint. I can easily do that using Alamofire's upload request as multipartFormData. However I want to get rid of Alamofire ...
Parve asked 4/10, 2019 at 11:49

4

Solved

I am trying to encode a struct struct Configuration : Encodable, Decodable { private enum CodingKeys : String, CodingKey { case title = "title" case contents = "contents" } var title : String...
Rehnberg asked 5/8, 2018 at 18:9

12

Solved

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...
Bessbessarabia asked 1/2, 2018 at 17:19

5

Solved

I'm trying to find the best way to Encode/Decode an array of structs conforming to a swift protocol using the new JSONDecoder/Encoder in Swift 4. I made up a little example to illustrate the probl...
Peregrination asked 8/6, 2017 at 16:34

© 2022 - 2024 — McMap. All rights reserved.