Questions tagged [codable]

Use this tag only for questions directly pertaining to the Swift Codable protocol introduced in Swift 4.

Serialization / deserialization protocol composed of Encodable and Decodable protocols. Introduced in Swift 4.

References

1568 questions
58
votes
4 answers

How to use swift 4 Codable in Core Data?

Codable seems a very exciting feature. But I wonder how we can use it in Core Data? In particular, is it possible to directly encode/decode a JSON from/to a NSManagedObject? I tried a very simple example: and defined Foo myself: import…
hgl
  • 2,034
  • 4
  • 21
  • 30
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
48
votes
3 answers

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

I am using Codable protocol from Swift 4 first time, I am not able to understand use of decodeIfPresent from Decodable. /// Decodes a value of the given type for the given key, if present. /// /// This method returns `nil` if the container does not…
technerd
  • 14,144
  • 10
  • 61
  • 92
47
votes
10 answers

Swift's JSONDecoder with multiple date formats in a JSON string?

Swift's JSONDecoder offers a dateDecodingStrategy property, which allows us to define how to interpret incoming date strings in accordance with a DateFormatter object. However, I am currently working with an API that returns both date strings…
RamwiseMatt
  • 2,717
  • 3
  • 16
  • 22
45
votes
4 answers

Swift 4 Codable; How to decode object with single root-level key

I'm using the Swift 4 Codable protocol with JSON data. My data is formatted such that there is a single key at the root level with an object value containing the properties I need, such as: { "user": { "id": 1, "username": "jdoe" } } I…
Joshua Breeden
  • 1,844
  • 1
  • 16
  • 29
42
votes
3 answers

Using JSONEncoder to encode a variable with Codable as type

I managed to get both JSON and plist encoding and decoding working, but only by directly calling the encode/decode function on a specific object. For example: struct Test: Codable { var someString: String? } let testItem =…
Denis Balko
  • 1,566
  • 2
  • 15
  • 30
41
votes
8 answers

How to conform UIImage to Codable?

Swift 4 has Codable and it's awesome. But UIImage does not conform to it by default. How can we do that? I tried with singleValueContainer and unkeyedContainer extension UIImage: Codable { // 'required' initializer must be declared directly in…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
40
votes
6 answers

Swift 4 Decodable - Dictionary with enum as key

My data structure has an enum as a key, I would expect the below to decode automatically. Is this a bug or some configuration issue? import Foundation enum AnEnum: String, Codable { case enumValue } struct AStruct: Codable { let dictionary:…
Chris Mitchelmore
  • 5,976
  • 3
  • 25
  • 33
38
votes
1 answer

Implementing a custom Decoder in Swift 4

I'd like to decode an XML document using the new Decodable protocol introduced in Swift 4, however, there doesn't seem to be an existing implementation for an XML decoder that conforms to the Decoder protocol. My plan was to use the SWXMLHash…
Toni Sučić
  • 1,329
  • 1
  • 13
  • 20
37
votes
4 answers

Codable 'has no initializers' in Xcode 9.3 (Swift 4.1)

After updating to Xcode 9.3 (which uses Swift 4.1), the following issue was found: Create an empty project, add a new .swift file to it and create two new classes: class CodableOne: Codable { let some: String } class CodableTwo: Codable…
EBDOKUM
  • 1,696
  • 3
  • 15
  • 33
37
votes
5 answers

Swift String escaping when serializing to JSON using Codable

I'm trying to serialize my object as following: import Foundation struct User: Codable { let username: String let profileURL: String } let user = User(username: "John", profileURL: "http://google.com") let json = try?…
user_4685247
  • 2,878
  • 2
  • 17
  • 43
35
votes
3 answers

Swift Codable Decode Manually Optional Variable

I have the following code: import Foundation let jsonData = """ [ {"firstname": "Tom", "lastname": "Smith", "age": "28"}, {"firstname": "Bob", "lastname": "Smith"} ] """.data(using: .utf8)! struct Person: Codable { let firstName,…
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
32
votes
3 answers

Protocol type cannot conform to protocol because only concrete types can conform to protocols

Within the app, we have two types of Stickers, String and Bitmap. Each sticker pack could contain both types. This is how I declare the models: // Mark: - Models protocol Sticker: Codable { } public struct StickerString: Sticker, Codable,…
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
29
votes
1 answer

iOS Generic type for codable property in Swift

I need to get a generic variable for a struct for parsing a JSON but there is an error that I am getting Type 'BaseJsonModel' does not conform to protocol 'Codable Below is my struct struct BaseJsonStruct: Codable { let info: String …
Ekra
  • 3,241
  • 10
  • 41
  • 61
29
votes
8 answers

Swift 4 JSON Decodable simplest way to decode type change

With Swift 4's Codable protocol there's a great level of under the hood date and data conversion strategies. Given the JSON: { "name": "Bob", "age": 25, "tax_rate": "4.25" } I want to coerce it into the following structure struct…
Dru Freeman
  • 1,766
  • 3
  • 19
  • 41