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
1
vote
2 answers

Swift Codable Protocol. String encoding issue

Recently I have faced an issue while encoding an entity, which conforms to Codable protocol. Here is the code for the Playground: class Person: Codable { var name: String? var age: Int? var isDev: Bool? } let p1 = Person() p1.name =…
Gasper J.
  • 481
  • 5
  • 11
1
vote
1 answer

Swift Decodable - how to avoid generic type?

I am retrieving a complex nested object from my JSON REST API. DocumentDraft - uuid: String - schema: Schema // Very complicated object with many variations - url: String - values: [Value] - successors: [String] - predecessors:…
Mundi
  • 79,884
  • 17
  • 117
  • 140
1
vote
2 answers

Decoding enum in Swift 5 - This CANNOT be this hard

I've been trying for the past 3-4 hours to get this stupid thing to decode this enum correctly and now am incredibly frustrated with this! I have a json string returning from an API that looks like this: [ { "contractType": 0 } ] I am…
Cory
  • 658
  • 3
  • 7
  • 19
1
vote
2 answers

How to add Dynamic key name and value in Encodable(Swift)?

I have a very simple request: {"token": "abcd", "key": "value" } I'm trying to add this request as an Encodable. Now, here the issue arises that the key name can be anything like "123", "311", the type of the key will be String, but it's name is…
Rob
  • 2,086
  • 18
  • 25
1
vote
1 answer

Type needs to conform to Encodable protocol even though it is already

Error: Instance method 'encodeIfPresent(_:forKey:)' requires that '[RelatedQuestions].Type' conform to 'Encodable' The object i have already conforms to the Codable protocol and it is still giving me the error that it is not. Why? func encode(to…
Kegham K.
  • 1,589
  • 22
  • 40
1
vote
1 answer

Swift encode class without nesting super class

Summary: I would like to encode all fields of my super class without nesting them in the json result. Here's what I mean: Let's say we have these structs: struct Toy: Codable { var name: String } class BasicEmployee: Codable { var name: String …
linus_hologram
  • 1,595
  • 13
  • 38
1
vote
1 answer

Multipart request - Encodable struct with UIImage

With Swift 5 I'm trying to cut a lot of my dependencies (Alamofire), and I'm trying to understand how can I do a multipart request while using Codable and URLRequest My code is working correctly for creating an user with a name and email, but I need…
jfredsilva
  • 1,434
  • 1
  • 9
  • 15
1
vote
1 answer

Implementing KeyedDecodingContainerProtocol for URL types

I'm working on a project to create a custom Decoder to deal with non JSON data. Data to decode can be URLs or Strings. For example: struct MyData: Decodable { let server: URL let version: String } In order to deal with such a type of…
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
1
vote
1 answer

JSON Encoding with snake_case conversion ignores CodingKeys

I have the following struct that I send to an endpoint: struct IdleAlarmRequest: Encodable { let idleAlarm: [IdleAlarmParameters] enum CodingKeys: String, CodingKey { case idleAlarm = "IdleAlarm" } } No matter what I do…
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
1
vote
2 answers

Errors when using Codable - Swift

I am using Codable to try to Encode JSON to a Model but I get two errors. Value of type 'KeyedEncodingContainer' has no member 'encoder' Here's my code: import UIKit struct NewCustomer : Codable { var firstName :String var lastName…
user11198154
1
vote
1 answer

How to JSON encode multiple date formats within the same struct

Need to encode into JSON, a struct that has 2 Date instance variables (day and time), however, I need to encode each date instance variable with a different format, ie. for "day":"yyyy-M-d" and "time":"H:m:s". Have written a custom decoder which…
Random Joe
  • 69
  • 6
1
vote
3 answers

Swift Codable: encode structure with dynamic keys

I want to have the following JSON structure (fake example): { "Admin" : { "name" : "John", "age" : "42" }, "Sales" : { "name" : "John", "age" : "42" }, "CEO" : { "name" :…
Freek Sanders
  • 1,187
  • 10
  • 26
1
vote
0 answers

Error Making Entity Relationship Encodable

I'm trying to get a simple NSManagedObject class to conform to Encodable so that I can easily encode it to JSON. I have an entity named TestObject that has a one to many relationship with an entity named Device. Everything works fine until I try to…
dubbeat
  • 7,706
  • 18
  • 70
  • 122
0
votes
2 answers

How to encode a dictionary of unknown/variable keys?

Say I have to send this data to the server: struct Event: Codable { let title: String let params: [String:Any]? //Not allowed } So for instance, these events could look like any of these: let event = Event(title: "cat was pet",…
soleil
  • 12,133
  • 33
  • 112
  • 183
0
votes
2 answers

Struct with protocol as property type does not conform to protocols 'Decodable' and 'Encodable'

I have a struct UISectionModel with only one property section, which has a protocol UISectionProtocol for a type, as follows: struct UISectionModel: Codable { let section: UISectionProtocol } Here is the implementation of the UISectionProtocol…
vintinhum
  • 19
  • 1