encodable Questions

3

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 erro...
Diatribe asked 6/5, 2018 at 23:19

13

Solved

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...
Galitea asked 17/1, 2018 at 9:7

6

Solved

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...
Electoral asked 27/6, 2018 at 8:42

3

Solved

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? JSO...
Bouilli asked 21/7, 2020 at 16:10

2

Solved

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 J...
Church asked 22/10, 2018 at 5:26

4

Solved

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 n...
Gopak asked 12/3, 2020 at 10:30

3

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: Str...
Radicle asked 30/9, 2019 at 21:52

2

Solved

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, JSONEncod...
Haunted asked 13/1, 2018 at 8:28

2

Solved

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 ca...
Buffy asked 6/2, 2020 at 13:48

1

Solved

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...
Homologize asked 18/7, 2017 at 14:10

2

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"...
Demilitarize asked 20/7, 2018 at 16:58

2

Solved

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 "data_fo...
Footsie asked 22/5, 2018 at 7:8

3

Solved

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 Mem...
Buchholz asked 28/3, 2018 at 15:28

2

Solved

I am trying to serialize a struct to a String using Swift 4's Encodable+JSONEncoder. The object can hold heterogenous values like String, Array, Date, Int etc. The used approach works fine with t...
Felten asked 7/2, 2018 at 7:55

2

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"...
Sonyasoo asked 31/1, 2018 at 14:6

2

Solved

I'm looking to store models objects in a Dictionary and would like to serialize the whole dictionary using JSONEncoder into data and subsequently into a string and save it. The idea is to use Swif...
Eratosthenes asked 20/1, 2018 at 16:21
1

© 2022 - 2025 — McMap. All rights reserved.