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 to Store Json Response Of Registration API ? and Getting error of "The data couldn’t be read because it isn’t in the correct format."

Hello I'm trying to store my json Response to My Model Class, But I'm getting an error "The data couldn’t be read because it isn’t in the correct format." My Api is working Perfectly fine, only issue is Not storing my data into Data Model…
iFateh
  • 570
  • 2
  • 6
  • 22
-1
votes
1 answer

String values cannot be encoded when app is running on iOS 12

I have written this func to save codable objects in my keychain store. I passed a String API key to this func as the value and in 13+ iOS versions it works perfectly fine. class StoreManager { public static func save(_ value: T,…
limbo
  • 329
  • 1
  • 11
-1
votes
1 answer

How to make a custom class Codable?

I'm using a thread safe dictionary found on Github But how to make the ThreadSafeDictionary in that repo conform to the Codable? It seems initialise a new lock in the encoder function would help?
anb
  • 265
  • 1
  • 8
-1
votes
1 answer

Fail to decode JSON because the model is Identifiable

I have a JSON like this [ { "name": "car", "color": "red" }, { "name": "bike", "color": "blue" }, ... etc ] and I have this model to use during the decode struct Element:Codable { var name:String? var color:String? } later in…
Duck
  • 34,902
  • 47
  • 248
  • 470
-1
votes
1 answer

Swift: encode [String:Any] to JSON

I have to encode a [String:Any] object to a JSON like this: { "timestamp": "2021-06-17T09:18:30.212Z", "readings": [ { "uuid": "string", "major": 0, "minor": 0, "macAddress": "string", "rssi": 0 } ] } I…
Luciano
  • 1,208
  • 2
  • 17
  • 36
-1
votes
1 answer

Swift Codable JSON parse error with JSONDecoder

I am trying to handle a JSON with Codable but there's a parsing error when I decode it with JSONDecoder().decode. { "data": [ { "id": "90", "symbol": "BTC", "name": "Bitcoin", "nameid":…
eemrah
  • 1,603
  • 3
  • 19
  • 37
-1
votes
1 answer

does not conform to protocol 'Decodable' and 'Encodable'

hi programmers actually when I format my json as codable I faced to this error " type 'HomeViewControler'does not conform to protocol 'Decodable' and 'Encodable' " I saw similar questions but I didn't result , I happy to see your help thanks here is…
Ali kazemi
  • 84
  • 7
-1
votes
1 answer

Create JSON data programmatically from API results in Swift

I am making a request to an API with the following json payload: var json: [String: Any] = [ "locations": [ [ "latitude" : 10, "longitude": 10, ], [ "latitude" : 20, …
Chris
  • 247
  • 1
  • 4
  • 17
-1
votes
2 answers

Codable with random elements in json

can you tell me how to parse this kind of json using Сodable? { "-MV_nbUZXH0YxpkMtdDK": { "log": "Installing dependencies", "ts": "2021-03-12T09:39:51.373513016Z" }, "-MV_nbUeK8yJbrTkyhku": { "log": "Python version set to 2.7", "ts":…
-1
votes
1 answer

CodingKey not found from a different struct during decoding

I am trying to use a parsed date string (dateString) from the Datum struct to build a new Date object in the AzimuthAtDate class... { "11": [{ "name": "Sun", "date": "2021-01-26", "rising": [{ "coord":…
Paul
  • 115
  • 2
  • 10
-1
votes
1 answer

Make Generic Class Codable

I have this class: class Course { var amount: Int var type: T init(amount: Int, type: T) { self.amount = amount self.type = type } } I don’t have access to Dish. But let’s assume this implementation: class…
fulty12
  • 3
  • 2
-1
votes
2 answers

Type '' does not conform to protocol 'Decodable'/'Encodable'

I have this code to get information from firestore: struct Spty: Identifiable, Codable{ @DocumentID var id: String? = UUID().uuidString var spty: String var r: NSNumber var g: NSNumber var b: NSNumber } class SptyViewModel:…
Mateus Neves
  • 674
  • 1
  • 6
  • 20
-1
votes
1 answer

How to type cast an Enum to a Double in Swift

I have a codable Enum, that can take the form of a string OR a double because the JSON response I get can either be in a string or a double. I need to extract the double from the enum but I can't figure out why. enum greeksEnum: Codable,…
Chris
  • 361
  • 1
  • 4
  • 17
-1
votes
2 answers

Filter an (Codable) array by another array

I'm trying to filter my json data by IDs (trying mark some favourites and filter using it) struct workoutList : Codable { let id : Int let title : String let tag : String } func selectedWorkoutGroup(libraryFilter: Int,…
app4g
  • 670
  • 4
  • 24
-1
votes
3 answers

Is this poorly formed JSON?

I am working through an online tutorial that makes use of the Whitehouse Petitions JSON feed. I am using Swift 5 and have implemented my structs to conform to Codable. The Whitehouse JSON causes a Codable error. Below is a snippet of the JSON. …
Scooter
  • 4,068
  • 4
  • 32
  • 47