Questions tagged [decodable]

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

678 questions
8
votes
3 answers

Handling JSON Array Containing Multiple Types - Swift 4 Decodable

I am trying to use Swift 4 Decodable to parse an array that contains two different types of objects. The data looks something like this, with the included array being the one that contains both Member and ImageMedium objects: { "data": [{ …
Grambo
  • 887
  • 8
  • 25
8
votes
2 answers

Fatal error: Dictionary does not conform to Decodable because Any does not conform to Decodable

I'm trying to use swift 4 to parse a local json file: { "success": true, "lastId": null, "hasMore": false, "foundEndpoint": "https://endpoint", "error": null } This is the function I'm using: func loadLocalJSON() { …
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
8
votes
1 answer

Using Decodable with inheritance raises an exception

I'm working against Rest API service, where the responses are divided into Base response, and all other responses inherit from it. I'm trying to building the same structure for my response model classes, using the Decoder interface. However i'm…
dor506
  • 5,246
  • 9
  • 44
  • 79
7
votes
2 answers

How to use Decodable in Swift?

I am using a free dates API in my project. I am using Decodable to parse the JSON data. Here I created my struct:- struct jsonStruct: Decodable { var message: Bool? var data: [dateData] } struct dateData: Decodable { var quarter: Int? var day:…
Nilu Sahani
  • 91
  • 1
  • 1
  • 9
7
votes
1 answer

Expected to decode Int but found a string

My JSON looks like: { "status": true, "data": { "img_url": "/images/houses/", "houses": [ { "id": "1", "name": "Kapital", "url": "https://kapital.com/", …
J. Doe
  • 521
  • 4
  • 15
7
votes
2 answers

Expected to decode Dictionary but found a string/data instead

I'm working with the docodable protocol and I'm having this error: typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [MakeApp_v2.Params.(CodingKeys in _244BBB2F32A8C5CF3DB84B0C6A94B232).config,…
riogarrell
  • 103
  • 1
  • 10
7
votes
1 answer

How to get the nondecoded attributes from a Decoder container in Swift 4?

I'm using the Decodable protocol in order to parse JSON received from an external source. After decoding the attributes that I do know about there still may be some attributes in the JSON that are unknown and have not yet been decoded. For example,…
tshaw
  • 73
  • 1
  • 5
6
votes
4 answers

Swift Decodable - How to decode nested JSON that has been base64 encoded

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": "eyAibmFtZSI6ICJzb21lLXZhbHVlIiB9", } PS…
Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90
6
votes
2 answers

How to notify or print for missing key on model class from API response in iOS Swift Codable?

I have one JSON response from API as follows, Previous JSON Response: [ { "EmployeeId": 711, "FirstName": "Steve", "LastName": "Jobs" }, { "EmployeeId": 714, "FirstName": "John", "LastName": "Doe" } ] and model class…
iAj
  • 3,787
  • 1
  • 31
  • 34
6
votes
4 answers

iOS Swift Decodable: Error: Cannot invoke initializer for type with no arguments

I'm getting error in initialising a struct, please see the screenshot attached below. After debugging I found that including review variable in the struct is giving problem. I can't figure out what I'm doing wrong. Can anyone help me out? Tx I'm…
Matt
  • 315
  • 2
  • 6
  • 20
6
votes
1 answer

Codable : does not conform to protocol 'Decodable'

Not able to figure why my class does not conform to Codable Please not that in my case I do not need to implement the methods encode and decode. public class LCLAdvantagePlusJackpotCache: Codable { public let token: String public let amount:…
user4422315
6
votes
2 answers

Swift Codable: How to encode top-level data into nested container

My app uses a server that returns JSON that looks like this: { "result":"OK", "data":{ // Common to all URLs "user": { "name":"John Smith" // ETC... }, // Different for each URL …
ABeard89
  • 911
  • 9
  • 17
6
votes
2 answers

Dictionary of String:Any does not conform to protocol 'Decodable'

I'm trying to implement a Decodable to parse a json request but the json request has a dictionary inside of the object. Here is my code: struct myStruct : Decodable { let content: [String: Any] } enum CodingKeys: String,…
user2924482
  • 8,380
  • 23
  • 89
  • 173
6
votes
1 answer

Parsing Nested JSON in SWIFT 4

I am currently parsing JSON with Decodable in SWIFT 4. The JSON is formatted as follows: { "autopayout_from": "1.010", "earning_24_hours": "9.74731104", "error": false, "immature_earning": 0.635593030875, "last_payment_amount":…
confusedChris
  • 93
  • 1
  • 4
  • 8
6
votes
3 answers

Swift 4 Decodable - decoding JSON object into `Data`

I have the following data structure: { "type": "foo" "data": { /* foo object */ } } Here's my class for decoding it: final public class UntypedObject: Decodable { public var data: Data enum UntypedObjectKeys: CodingKey { …
Eimantas
  • 48,927
  • 17
  • 132
  • 168
1 2
3
45 46