Questions tagged [codable]

Please utilize this tag exclusively for inquiries specifically related to the Swift Codable protocol that was introduced in Swift 4.

Encoding a struct in Swift using Encodable and incorporating a pre-encoded value

Consider a data structure with a unique setup, where the contents holds an encoded JSON fragment. let partial = """ { "foo": "Foo", "bar": 1 } """ struct Document { let contents: String let other: [String: Int] } let doc = Document(contents: partial ...

Mapping an array element to a string using Codable object

I am facing a peculiar issue where my back-end returns an object structured like this: { "user": { "name": [ "John" ], "familyName": [ "Johnson" ] } } Each property is presented as an array cont ...

Troubleshooting JSON Parsing in Swift 4 - Can You Help Me Find the Issue?

Having trouble with Swift 4 JSON Parsing Int issue. I've noticed that in all the examples I've seen, Int is encoded / decoded out of the box. Can anyone point out what mistake I might be making? Thank you! JSON let patientBundleEntry = """ { "resourceT ...

Unexpected error: The 'try!' expression has caused a fatal error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "hours", intValue: nil)

I encountered a strange error that I am unable to comprehend. The issue arose only after adding a new item. Here is the error message: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stri ...

Unlocking JSON with various structures using Swift

I am dealing with json data that follows a consistent structure from a nosql database (PK, SK, attributes). The content of the attributes section varies depending on the value of SK. For instance: [ { "PK": "POLL#1544693DF0E88EC-3225- ...

Struggling with JSON decoding in Swift? Unable to read the data as it is not in the right format?

I seem to be stuck trying to decode a JSON file. The error message I keep getting says: "The data couldn’t be read because it isn’t in the correct format." I'm struggling to identify what's causing the issue. Would you kindly take a look? En ...

Inconstant API causing issues in using Swift's Decodable

Is it possible to handle the decoding of JSON into a struct when the API sending the JSON may not be consistent with its typing? For example, sometimes a property is sent as an array and other times as a string. I'm unsure how to manage this or if the ...

Swift4 JSON Parsing Tool

Looking to convert a JSON file into a Codable struct in Swift4 for iOS11.1 and Xcode9.1, Below is my code: struct Station: Codable { let htmlAttributions: [String] let nextPageToken: String let status: String struct Result: Codable { ...