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

Custom Swift Encoder/Decoder for the Strings Resource Format

I have been playing around with Codable and reading and writing JSON from and to a file. Now I would like to write a custom Coder that can read and write iOS .strings files. Can anyone help me with this? I found the protocols Encoder and Decoder,…
Banana
  • 4,010
  • 9
  • 33
  • 49
55
votes
13 answers

How to use Any in Codable Type

I'm currently working with Codable types in my project and facing an issue. struct Person: Codable { var id: Any } id in the above code could be either a String or an Int. This is the reason id is of type Any. I know that Any is not…
PGDev
  • 23,751
  • 6
  • 34
  • 88
18
votes
2 answers

Inheritance of Encodable Class

I am writing a program using Swift 4 and Xcode 9.2. I have faced difficulties with writing encodable class (exactly class, not struct). When I am trying to inherit one class from another, JSONEncoder does not take all properties from sub class…
Alex
  • 1,038
  • 2
  • 12
  • 32
17
votes
6 answers

Why can't we use protocol `Encodable` as a type in the func?

I'm trying to get data by encode model which conforms to Encodable protocol. But it's failed to invoke func encode like code below: // MARK: - Demo2 class TestClass2: NSObject, Encodable { var x = 1 var y = 2 } var dataSource2:…
AnZ
  • 185
  • 1
  • 1
  • 9
13
votes
2 answers

Converting Codable/Encodable to JSON object swift

Recently i incorporated Codable in a project and to get a JSON object from a type conforming to Encodable i came up with this extension, extension Encodable { /// Converting object to postable JSON func toJSON(_ encoder: JSONEncoder =…
Kamran
  • 14,987
  • 4
  • 33
  • 51
10
votes
2 answers

Encode a [String: Encodable] dictionary into JSON using JSONEncoder in Swift 4

I am just curious how can I encode a dictionary with String key and Encodable value into JSON. For example: let dict: [String: Encodable] = [ "Int": 1, "Double": 3.14, "Bool": false, "String": "test" ] The keys in this dict are all…
ylorn
  • 241
  • 2
  • 8
10
votes
3 answers

Make a protocol Codable and store it in an array

I have the Animal protocol with 2 structs that conform to it and a Farm struct which stores a list of Animals. Then, I make them all conform to Codable to store it in a file, but it throws the error cannot automatically synthesize 'Encodable'…
ngngngn
  • 579
  • 4
  • 18
8
votes
4 answers

Swift Encodable, Decodable Vs Codable

Found in Apple doc, that Codable protocol is composed of Encodable and Decodable. Thus, Codable = Encodable & Decodable Now, let's say I implemented below classes, class X: Codable { var name: String } class Y: Encodable, Decodable { var…
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
8
votes
3 answers

How to encode struct in Swift using Encodable that contains an already encoded value

Imagine a data structure as follows, containing a value in contents that is an already encoded JSON fragment. let partial = """ { "foo": "Foo", "bar": 1 } """ struct Document { let contents: String let other: [String: Int] } let doc =…
klotz
  • 1,951
  • 2
  • 22
  • 27
8
votes
3 answers

Handling JSON Array Containing Multiple Types - Swift 4 Decodable

I am trying to use Swift 4 Decodable to parse an array that contains two different types of objects. The data looks something like this, with the included array being the one that contains both Member and ImageMedium objects: { "data": [{ …
Grambo
  • 887
  • 8
  • 25
6
votes
2 answers

Swift Codable: How to encode top-level data into nested container

My app uses a server that returns JSON that looks like this: { "result":"OK", "data":{ // Common to all URLs "user": { "name":"John Smith" // ETC... }, // Different for each URL …
ABeard89
  • 911
  • 9
  • 17
6
votes
2 answers

How to encode Dictionary with JSONEncoder in Swift 4

I want to encode Dictionary to json with JSONEncoder. It seems like a Request, receive a dictionary as parameter and encode it to json as http body. The code is looks like this: let dict = ["name": "abcde"] protocol Request { var params:…
鸡肉味嘎嘣脆
  • 233
  • 1
  • 3
  • 15
5
votes
3 answers

Fastest way to save structs iOS / Swift

I have structs like struct RGBA: Codable { var r: UInt8 var g: UInt8 var b: UInt8 var a: UInt8 } I want save large amount of this structs (>1_000_000) Decode guard let history = try? JSONDecoder().decode(HistoryRGBA.self,…
KLASTER
  • 95
  • 6
5
votes
2 answers

struct with generic property conforming to Encodable in Swift

I was looking for, in a struct, having a way of having a generic property where the type is defined at runtime like: struct Dog { let id: String let value: ?? } A simple use case where this can be useful is when building a json object. A…
Florian Ldt
  • 1,125
  • 3
  • 13
  • 31
4
votes
1 answer

How to decode an array of inherited classes in Swift

The problem: decode an array of objects belonging to Parent and Child classes. I read a lot of stuff on this subject but I have not been able to find a simple solution. I encoded a type property which provide the information of the original class,…
Fab
  • 1,468
  • 1
  • 16
  • 37
1
2 3 4 5 6 7