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
0 answers

Encode array itself of Codable objects to [String: Any]

I want to send array of Codable objects. So I've created a class which conforms to Codable protocol: class ResultAnswer: Codable { var id: Int var closed: [Int]? var open: String? init(id: Int) { self.id = id } } When I…
kkiermasz
  • 464
  • 2
  • 15
0
votes
2 answers

Declare empty dictionary in codable class

I want to send empty Dictionary like this “visitor_attrs”: {} I try to implement empty dictionary in a class. In the decoder I get the warning: No 'decode' candidates produce the expected contextual result type 'Dictionary' How can I do this? var…
Ali Ihsan URAL
  • 1,894
  • 1
  • 20
  • 43
0
votes
1 answer

get error 401 when post request using XMLParsing swift library

i'm testing XMLParsing library (that use Codable protocol with XML request) XMLParsing library link : https://github.com/ShawnMoore/XMLParsing with https://openweathermap.org API the API link is "http://api.openweathermap.org/data/2.5/weather" my…
Basel
  • 550
  • 8
  • 21
0
votes
1 answer

Cannot convert value of type '[[ModelName]?]' to expected argument type '[ModelName].Type'

I have created a model class from [https://jsonmaster.github.io/#][1] this link in codable, I am getting this kind of error Cannot convert value of type '[[BookingMasterModel]?]' to expected argument type '[BookingMasterModel].Type' Actually I am…
prachit
  • 365
  • 3
  • 10
0
votes
2 answers

Using JSONEncoder for Equatable method in Swift

I know JSON keys don't have any order and can be generated randomly, but there isn't anything preventing me from writing this function and from my tests this works on my every use case I tested. func == (lhs: T, rhs: T) -> Bool { …
Ali Riahipour
  • 524
  • 6
  • 20
0
votes
1 answer

Using JSON Encoder with a computed variable of type Codable

With my newbie swift skills I'm struggling to figure out the correct swift syntax to get this playground to work. Depending how I try to solve it I either get Cannot invoke 'encode' with an argument list of type '(Encodable)' which is similar…
300baud
  • 540
  • 4
  • 17
0
votes
1 answer

How to parse an image from JSON into swift4

I am trying to pull images from my database in swift. I can retrieve the price, video, and p_name but not the images. I would like to update the updateImage UIImageView with the image from the database but I get two errors. Error one says: Type…
pillarman38
  • 47
  • 3
  • 13
0
votes
1 answer

Custom structure using jSONEncoder

Want to encode an object into a custom structure using JSONEncoder+Encodable. struct Foo: Encodable { var name: String? var bars: [Bar]? } struct Bar: Encodable { var name: String? var value: String? } let bar1 = Bar(name: "bar1",…
justintime
  • 352
  • 3
  • 11
0
votes
1 answer

What is the most elegant way to create nested json using swift 4?

What is the most elegant way to create JSON from this structure with the only one parameter struct SessionStorage: Encodable { var value: String func encode(to encoder: Encoder) throws { var container =…
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
-1
votes
1 answer

How to use different keys in input and output when using Encodable protocol?

Expected this: name -> fullname struct Person: Codable { let name: String enum CodingKeys: String, CodingKey { case name = "fullname" } init(from decoder: Decoder) throws { let container = try…
János
  • 32,867
  • 38
  • 193
  • 353
-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

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

Swift: custom key-value encoding with Encodable conformance in struct

struct Struct: Encodable { let key: String let value: String } let aStruct = Struct(key: "abc", value: "xyz") Given this struct and the default Encodable conformance provided, JSON encoding produces { key = abc; value = xyz; } whereas…
Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
-4
votes
2 answers

Post request in swift with encodable

I want to create a post request in swift but i got confused in Encodable protocols. and how to create them. below is the data that i want to post : { answers: [{ "time": 1, "score": 20, "status": true, …
AMAN SHARMA
  • 21
  • 1
  • 5
1 2 3 4 5 6
7