Questions tagged [encodable]

Use this tag only for questions directly pertaining to the Swift standard Encodable protocol

It is the standardized protocol (defined by Swift standard library) for type that can encode itself to an external representation

References

104 questions
0
votes
1 answer

Unable to parse JSON data properly from Alomafire

I know there are various threads on this but it seems like there's a fundamental flaw in my understanding of objects in Swift. So I have the following function that returns a JSON response as expected: func executeGet( completion: @escaping…
Mansidak
  • 136
  • 6
0
votes
1 answer

Argument passed to call that takes no arguments error for Structs within Structs

Hi all so I need to call a function that takes in the following parameters: let params = [ "inputParameters" : ["A2":"a1","B1":"b1","C1":"c1","D1":"d1"] ] so I made the following encodable struct for it struct…
Mansidak
  • 136
  • 6
0
votes
1 answer

Swift, OptionSet, Encodable: Need help writing a custom encoder for OptionSet to generate user friendly JSON file

I have an application that stores some configuration options which I'd like to write out to a JSON file. Simplified version of my app's config/options structs and JSON encoding ... struct AppOptions: OptionSet, Encodable { let rawValue: Int …
KieranC
  • 57
  • 6
0
votes
0 answers

how to reduce the Coddle protocol for different payload json in swift

I have bunch of json string that need to format as payload for different request api call. I have no problem to get correct response with following code func request(url: String, method: String = "POST", body: Encodable? = nil, session:…
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
0
votes
0 answers

ignoe/bypass the key if value is nil after decoder in swift

My process is I have bunch of json string that I need to convert into request payload and make a POST request to api call. The issue is key may or may not exist from my original json string and the API does not accept nil as value. So the API call…
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
0
votes
2 answers

How to create encodable request in swift,when my request is present in Json?

I want create encodable struct request for the following JSON {"Symbols":[{"Name":"AAS1"},{"Name":"ASSD"}],"NoOfSymbols":2,"msgtype":15} I tried to create but getting error.Type 'SymbolName' does not conform to protocol 'Encodable'.Given my tried…
Manish Kumar
  • 997
  • 2
  • 13
  • 30
0
votes
0 answers

swift geoJSON can not encode

I'm trying to get geoJSON: import GeoJSON import MapKit let coordArray: [CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: 77, longitude: 44), CLLocationCoordinate2D(latitude: 78, longitude: 45), CLLocationCoordinate2D(latitude: 79,…
kadi4
  • 5
  • 2
0
votes
1 answer

Protocol with Encodable protocol

I have struct that has parameter with protocol type protocol CheckoutOrder {} struct CheckoutOrderEntity: Encodable { private enum CodingKeys: String, CodingKey { case amount case payment } let amount: Int let…
0
votes
0 answers

How to decode in model when custom object's key missing from json

I have a Json in which there is the possibility that a few keys can be missing Full JSON looks like this { "data": "some text", "id": "3213", "title": "title", "description": "description", "customObj1": [{ "id": "2423", …
Vineet Rai
  • 80
  • 7
0
votes
1 answer

How to create a nested Encodable class and return it from a function in Swift?

In my iOS project, I want to do the following: create an Encodable class (named ChannelAnswer) which has multiple attributes, including another generic Encodable object pass an instance of that class as an argument of a function or return it from a…
matteoh
  • 2,810
  • 2
  • 29
  • 54
0
votes
1 answer

Swift Attempt to a decode a Codable parent class as one of its subclasses

Say I have parent class A: Codable with subclasses B1: A and B2: A. A different class Main: Codable in my application has a pointer to an A which can either be a B1 or a B2 but cannot be an A (I'm effectively treating A as an abstract class). When I…
Evan Kaminsky
  • 695
  • 10
  • 23
0
votes
1 answer

Creating Encodable class by injecting class conforming only to Encodable protocol

I am building network module which uses Body class that will be encoded to serve as body of url request. I want to init Body with any class that is conforming to Encodable protocol, in my case it is MyDevice class conforming to DeviceEncodable,…
Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19
0
votes
0 answers

Understanding the Codable protocol

I'm curious to understand the workings of the Codable protocol and how at compilation time it checks if the children properties of the object are Codable as well. I'm looking for ways to incorporate that into my own protocols and any insights into…
BJ Beecher
  • 195
  • 1
  • 5
0
votes
1 answer

Swift - Codable struct with generic Dictionary var?

I have a Swift struct that looks like this: struct MyStruct: Codable { var id: String var name: String var createdDate: Date } To that, I would like to add another property: a [String:Any] dictionary. The result would look like…
Shadowman
  • 11,150
  • 19
  • 100
  • 198
0
votes
2 answers

How to get Firebase data as a model in swift?

I am trying to get data from firebase and use it as a model that I created. Here is my model; class UserData{ var nickname : String = "" var onesignal_player_id : String = "" var step_count : Int = 0 var total_point : Int = 0 var competitions :…