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
26
votes
3 answers

Swift Decodable Optional Key

(This is a follow-up from this question: Using Decodable protocol with multiples keys.) I have the following Swift code: let additionalInfo = try values.nestedContainer(keyedBy: UserInfoKeys.self, forKey: .age) age = try…
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
23
votes
2 answers

How to use Codable protocol in objective c data model class?

I am working on mix and match iOS source code. I have implemented codable for swift data model class which reduces the burden of writing parser logic. I tried to conform my objective c class to codable protcol which in turn thrown an error "Cannot…
myCmyL
  • 435
  • 2
  • 4
  • 11
22
votes
4 answers

Expected to decode Int but found a number instead

I had issue with JSON parsing in Swift 4.2. Here is the following code which shown runtime error. My Json data is as follow which i got from server. { code: 406, message: "Email Address already Exist.", status: 0 } I am using Codable…
Jatin Chauhan
  • 1,107
  • 1
  • 8
  • 21
22
votes
3 answers

Swift Codable with dynamic keys

I have JSON structure as: "periods": { "2018-06-07": [ { "firstName": "Test1", "lastName": "Test1" } ], "2018-06-06": [ { "firstName": "Test1", "lastName": "Test1" } ] } I…
Masta
  • 461
  • 1
  • 3
  • 7
22
votes
2 answers

Swift Codable null handling

I have a struct that parse JSON using Codable. struct Student: Codable { let name: String? let amount: Double? let adress: String? } Now if the amount value is coming as null the JSON parsing is failing. So should I manually handle the…
Ekra
  • 3,241
  • 10
  • 41
  • 61
22
votes
1 answer

Codable and XMLParser in Swift

Using the new Swift4 Codable protocol works well for JSON-decoding (as explained here or here or in many other contributions). However, as it comes to XML-parsing, I couldn't find any information on whether this Codable protocol could also be used…
iKK
  • 6,394
  • 10
  • 58
  • 131
21
votes
1 answer

How to use computed property in a codable struct (swift)

I've created a "codable" struct to serialize a data set and encode it to Json. Everything is working great except the computed properties don't show in the json string. How can I include computed properties during the encode phase. Ex: struct…
monroo
  • 429
  • 3
  • 10
21
votes
3 answers

Init an object conforming to Codable with a dictionary/array

Primarily my use case is to create an object using a dictionary: e.g. struct Person: Codable { let name: String } let dictionary = ["name": "Bob"] let person = Person(from: dictionary) I would like to avoid writing custom implementations…
Chris Mitchelmore
  • 5,976
  • 3
  • 25
  • 33
21
votes
4 answers

How to manually decode an an array in swift 4 Codable?

Here is my code. But I do not know what to set the value to. It has to be done manually because the real structure is slightly more complex than this example. Any help please? struct Something: Decodable { value: [Int] enum CodingKeys:…
swift nub
  • 2,747
  • 3
  • 17
  • 39
20
votes
2 answers

Swift Codable multiple types

I try to parse an api returning a json object. My problem is that some keys are sometime a string, sometime an object like the key "Value" in the following example: [ { "Description": null, "Group": "Beskrivning av enheten", …
Sylvain
  • 509
  • 1
  • 7
  • 17
20
votes
1 answer

The `convertFromSnakeCase` strategy doesn't work with custom `CodingKeys` in Swift

I try to use Swift 4.1's new feature to convert snake-case to camelCase during JSON decoding. Here is the example: struct StudentInfo: Decodable { internal let studentID: String internal let name: String internal let testScore: String …
Howard
  • 268
  • 2
  • 7
20
votes
1 answer

Swift Codable expected to decode Dictionarybut found a string/data instead

I have been working with the Codable protocol Here is my JSON file : { "Adress":[ ], "Object":[ { "next-date":"2017-10-30T11:00:00Z", "text-sample":"Some text", "image-path":[ …
user7219266
19
votes
2 answers

Printing DecodingError details on decode failed in Swift

I'm starting to rewrite an application and I want to use Swift 4 Codable protocol to automatically convert json string to Objects and Structs. Sometimes, specially at the beginning of coding, I encountered decoding problems, so I want to print these…
smukamuka
  • 1,442
  • 1
  • 15
  • 23
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
18
votes
1 answer

How to handle partially dynamic JSON with Swift Codable?

I've got some JSON messages coming in over a websocket connection. // sample message { type: "person", data: { name: "john" } } // some other message { type: "location", data: { x: 101, y: 56 } } How can I convert those…
zemirco
  • 16,171
  • 8
  • 62
  • 96