Questions tagged [codable]

Use this tag only for questions directly pertaining to the Swift Codable protocol introduced in Swift 4.

Serialization / deserialization protocol composed of Encodable and Decodable protocols. Introduced in Swift 4.

References

1568 questions
18
votes
4 answers

Swift 4 Codable Array's

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 trying to envision how to use the new codable…
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
17
votes
4 answers

Use Swift's Encodable to encode optional properties as null without custom encoding

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 means that values that are nil are excluded from the…
heyfrank
  • 5,291
  • 3
  • 32
  • 46
16
votes
4 answers

How to conform an ObservableObject to the Codable protocols?

In SwiftUI beta 5, Apple introduced the @Published annotation. This annotation is currently blocking this class from conforming to the Codable protocols. How can I conform to these protocols so I can encode and decode this class to JSON? You can…
16
votes
2 answers

Type 'X' does not conform to protocol 'Encodable'

I am hoping here to get an understanding of this error and perhaps a broader understanding of encodable and decodable. Part of my class looks as follows: public var eventId: String? public var eventName: String? public var eventDescription:…
AlexK
  • 336
  • 8
  • 21
  • 41
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
16
votes
3 answers

Swift 4 Codable: Converting JSON return String to Int/Date/Float

I'm going through some projects and removing JSON parsing frameworks, as it seems pretty simple to do with Swift 4. I've encountered this oddball JSON return where Ints and Dates are returned as Strings. I looked at GrokSwift's Parsing JSON with…
Adrian
  • 16,233
  • 18
  • 112
  • 180
15
votes
5 answers

Convert received Int to Bool decoding JSON using Codable

I have structure like this: struct JSONModelSettings { let patientID : String let therapistID : String var isEnabled : Bool enum CodingKeys: String, CodingKey { case settings // The top level "settings" key } // The…
Hondus
  • 292
  • 1
  • 4
  • 16
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
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
3 answers

Swift: There is way to debug decodable object quickly

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 all the fields and putting it back one after the…
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46
13
votes
6 answers

What is the simplest way to instantiate a new Codable object in Swift?

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 parameter 'from' in call All I want is to insert a new…
ikevin8me
  • 4,253
  • 5
  • 44
  • 84
13
votes
5 answers

How to implement Codable while using Realm

Hy I am working on app that uses Realm and Alamofire. I am really happy in using these library in my iOS project. But then I have to post a List of models that contains multiple lists of models. So that is too much deep thing I mean List inside…
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
13
votes
2 answers

Converting Codable/Encodable to JSON object swift

Recently i incorporated Codable in a project and to get a JSON object from a type conforming to Encodable i came up with this extension, extension Encodable { /// Converting object to postable JSON func toJSON(_ encoder: JSONEncoder =…
Kamran
  • 14,987
  • 4
  • 33
  • 51
13
votes
2 answers

How to add custom transient property in Codable struct

I have following Codable struct that working as expected struct VideoAlbum: Codable { let id, image: String? let video, mediaType: JSONNull? let type, deleted, createdOn: String? let modifiedOn: JSONNull? enum CodingKeys: String, CodingKey…
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
13
votes
1 answer

Convert string to Date/Int/Double using codable

I am getting a response from an API but the problem is that the API is sending values back as a string of dates and doubles. I am therefore getting the error "Expected to decode Double but found a string/data instead." I have structured my struct…
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58