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

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'

I have built a set of API functions which encode an object (using a Struct which conforms to Codable), then Posts the resulting JSON Data object to a server, then decodes the JSON response. All works fine - especially happy with the new method for…
MarkCoolski
  • 88
  • 1
  • 6
2
votes
1 answer

Swift Codable: Is it possible to encode a structure one level up from within its own "encode(to:)" function?

I'm using a protocol to encode the conforming structures: protocol RequestParameters: Encodable { } extension RequestParameters { func dataEncoding() -> Data? { guard let data = try? JSONEncoder().encode(self) else { return nil } …
Pochi
  • 13,391
  • 3
  • 64
  • 104
2
votes
2 answers

Error Making Entity Relationship Encodable/ Decodable

How to encode/decode NSOrderedSet variable in swift4 codable while mapping with coredata NSManagedObject? import CoreData import Foundation @objc(TestObject) public class TestObject:NSManagedObject,Encodable { @nonobjc public class func…
imgroot
  • 131
  • 3
  • 11
2
votes
2 answers

using Anyobject with Codable in Swift 4

I am trying to parse JSON using Codable in swift 4. My concern is public class entStatusAndDescription : NSObject { var status : Int? var statusDescription : String? var records : AnyObject? } I want the above entity to be codable…
2
votes
1 answer

Encodable var in encodable struct

I want to create a struct that will contain a dictionary of encodable items, and itself be encodable. This is because I don't know the exact data structure, it will depend on connected database. I tried to do something like this: struct UserInfo:…
Damian Dudycz
  • 2,622
  • 19
  • 38
1
vote
1 answer

Swift custom encode for encoding Int time like pretty string

I have a custom object stored in a Struct: struct Event { let type: String let value: String let time: Int } where time is a UnixTime in millis. In the App I have a scrollable text field when I print this event in json format { "type" :…
Fry
  • 6,235
  • 8
  • 54
  • 93
1
vote
1 answer

Add Encodable extension to Swift nested enum

I want to encode instances of the existing NWInterface class which has a type field of type NWInterface.InterfaceType which is an enum. Is there a way to easily add the Encodable interface to that nested type? At the moment my workaround is to…
Alnitak
  • 334,560
  • 70
  • 407
  • 495
1
vote
1 answer

JSONEncoder encodes nil values when dealing with generic types

Summary: JSONEncoder unexpectedly encodes nil values as null when working with generic types that wrap over a protocol. Here's a short code that reproduces the problem: protocol Event { associatedtype Contents: Encodable var name: String {…
Cristik
  • 30,989
  • 25
  • 91
  • 127
1
vote
1 answer

Swift: Using multiple CodingKey types for the same Encodable struct

I'm struggling to write a single function that encodes the following struct: struct Lookup: Encodable { var id: Int var name: String enum StateCodingKeys: String, CodingKey { case id = "stateId" case name =…
JAHelia
  • 6,934
  • 17
  • 74
  • 134
1
vote
1 answer

How to save a CapturedRoom using NSCoder

I'm trying to build an app that creates a floor plan of a room. I used ARWorldMap with ARPlaneAnchors for this but I recently discovered the Beta version of the RoomPlan API, which seems to lead to far better results. However, I used te be able to…
Dennis
  • 53
  • 4
1
vote
2 answers

Is it possible to force encode Date as Date type instead of string?

I am trying to mock Apollo Queries using its init. It pretty much is taking in a dictionary to build the object up. public init(unsafeResultMap: [String: Any]) { self.resultMap = unsafeResultMap } So, I have decided to create Mock objects that…
Mocha
  • 2,035
  • 12
  • 29
1
vote
0 answers

Codable property within Struct doesn't allow the Struct to conform to Codable

I have a struct that I need to conform to 'Codable'. The struct itself must have an object within it that can also conform to 'Codable'. However, this prevents the struct from conforming to 'Codable'itself. Example: import Foundation struct…
ibmaul
  • 63
  • 5
1
vote
1 answer

Swift codable recursive enum with dynamic keys

I am trying to write a struct for dynamic data. The keys of the data are unknown as are their values. The struct looks like so: enum EntryData: Codable { case string(String) case array([EntryData] case nested([String: EntryData]) } struct…
David Wolters
  • 336
  • 2
  • 6
  • 18
1
vote
2 answers

How to convert an array of multiple object types to JSON without missing any object attribute in Swift?

In my iOS Swift project, I need to convert objects to JSON. I have one simple class: class Car : Encodable { var brand: String init(brand: String) { self.brand = brand } } and one subclass: class SUVCar : Car { var…
matteoh
  • 2,810
  • 2
  • 29
  • 54
1
vote
1 answer

How to decode single unusual property among many Decodable Swift?

I have a struct conforming to Decodable. It has 50 String properties and only one Bool. That bool is coming from server like a string "false"/"true" or sometimes like as integer 0/1, so cannot be decoded from the box. How can I make it decoding but…
bodich
  • 1,708
  • 12
  • 31