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
-1
votes
1 answer

How can I make a struct to decode JSON structure in Swift?

How can I make a struct or class to decode following type of json structure in swift? I want to extract mainly the report node data ! { "earliest": "2020-10-17", "latest": "2020-10-28", "report": { "purchase": { …
-1
votes
2 answers

Decode JSON with different structures with Swift

I have json data with the same core structure from a nosql database (PK, SK, attributes). The attributes part will be different depending on the value of SK. Example: [ { "PK": "POLL#1544693DF0E88EC-3225-410E-B156-D13781B238F6", "SK":…
mrshanes
  • 9
  • 2
-1
votes
1 answer

Swift - Codable Decode array of arrays of CLLocation

I have a struct that contains an array of arrays of CLLocations. This is to support a multipolyline (in other words, a bunch of potentially discontiguous lines). I wish to encode and decode this data. I am having trouble writing the encoding and…
jaredrada
  • 1,130
  • 3
  • 12
  • 25
-1
votes
1 answer

Swift Codable: Cannot decode dictionary of type [String: Any] or [String: Decodable]

In my custom initializer I'd like to decode a dictionary from JSON and then assign its values to properties in the class. To my surprise, compiler does not allow me to decode the dictionary, I get the error: Value of protocol type 'Any' cannot…
Adam
  • 1,776
  • 1
  • 17
  • 28
-1
votes
1 answer

how to create a codable structure for the following API data

{ "status": true, "error": false, "data": [ { "type": 0, "title": "", "description": "", "data": [ { "images":…
neethu
  • 1
-1
votes
2 answers

How to parse this JSON format in swift

I have this JSON format: { "version":"7.0.19", "fields":…
matarali
  • 103
  • 2
  • 8
-1
votes
1 answer

SWIFT: Trying to decode JSON ,it returns nil

When you i try to call .decode() to decode a struct, it returns nil.Can anyone help me? My app is returning null value from the JSON data, from this line of code: let newPosData = try JSONDecoder().decode(NewPosDataBase.self, from:…
Duffy DD
  • 51
  • 9
-1
votes
2 answers

Swift decoding JSON which has two possible types for the same field

I am dealing with this JSON using Alamofire and Codable: [ { "pID": "37229890-dcd8-36c4-bb63-e7b174aafeb7", "type": "FIRST", "content": { "id": "ff64", "ret": { "name": "A", …
Andoni Da Silva
  • 1,161
  • 15
  • 31
-1
votes
1 answer

Swift - How to JSONDecode with codable for JSON of nested arrays (where arrays without keys or names as per example given)

I am new to Swift and Codable approach. I have to decode the following JSON structure, through Codable approach in Swift project. [ [ [ { "id": "58", "parentCat": "7", …
SHS
  • 1,414
  • 4
  • 26
  • 43
-1
votes
2 answers

init(from decoder: Decoder) is causing a "Cannot infer contextual base" error in separate convenience init

I'm trying to subclass a Codable class and it's working fine until I add a init(from decoder: Decoder) function. Then, the compiler is giving me 2 errors on my convenience init: Cannot infer contextual base in reference to member 'geometry' Extra…
zakdances
  • 22,285
  • 32
  • 102
  • 173
-1
votes
1 answer

Codable for json response getting an error while parsing

After constructed the codable, getting an error. "msg": { "success": [ "Successfully logged in." ] }, "messages": [] } // Constructed the codable for the parsing response data struct Msg:Codable{ let…
kiran
  • 4,285
  • 7
  • 53
  • 98
-1
votes
2 answers

Difficulty parse integer value in JSON using JSON Decoder in Swift

I am trying to decode some JSON from an API that looks like this (foo is short for a list of properties): {"page":1,"total_results":10000,"total_pages":500,"results":[{"foo":"bar"},{"foo":"bar2"},{"foo":"bar3"}]} The struct recommended by…
user6631314
  • 1,751
  • 1
  • 13
  • 44
-1
votes
1 answer

How can I create JSON string from model data Swift?

How can I create JSON string from other model data? I am looking to create json string from data but not with success. Results I am getting in array and then I have used For Loop to pass to AppointmentDownloadModel for json creation. How can I pass…
Tekhe
  • 149
  • 1
  • 12
-1
votes
1 answer

How to get firstindex of array in struct Swift

Hi I have a struct like this struct OrderCaches: Codable { var food, drink: [Food]? } struct Food: Codable{ var id: Int? var name: String? var quantity: Double? var notes: String? } I want to get first index where id =…
TheCesco1988
  • 280
  • 1
  • 2
  • 10
-1
votes
1 answer

Map multiple keys to single coding key swift

I have the sample JSON below. There are multiple items but the keys for each item is different, but each parameter ends with same value like (ID,name,url,price,category,details).Is there any way that I can map all these items to single…
manideep
  • 37
  • 8