codable

Make class conforming to Codable by default in Swift

我的未来我决定 提交于 2019-12-08 07:54:45
问题 Such feature of Swift as Codable ( Decodable & Encodable ) protocol is very useful. But I found such issue: Let we have class Parent conforming to Codable : class Parent: Codable { var name: String var email: String? var password: String? } Ok, that class is conforming to Codable protocol "from the box", you don't need write any initializers, it's ready to be initialized from JSON like that: { "name": "John", "email": "johndoe@yahoo.com", "password": <null>} But let's say we need other class,

Using Decodable protocol with multiples keys

霸气de小男生 提交于 2019-12-08 05:01:02
问题 Say I have the following code: import Foundation let jsonData = """ [ {"firstname": "Tom", "lastname": "Smith", "age": {"realage": "28"}}, {"firstname": "Bob", "lastname": "Smith", "age": {"fakeage": "31"}} ] """.data(using: .utf8)! struct Person: Codable { let firstName, lastName: String let age: String? enum CodingKeys : String, CodingKey { case firstName = "firstname" case lastName = "lastname" case age } } let decoded = try JSONDecoder().decode([Person].self, from: jsonData) print(decoded

Codable Error - Expected to decode Dictionary<String, Any> but found an array instead

时光怂恿深爱的人放手 提交于 2019-12-08 02:03:42
问题 I am trying to decode a JSON with the help of codable - https://pastebin.com/Xfjj2XiP However I'm getting this error when I do so. typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "txt_forecast", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil)) Here's the code I'm using: struct container: Decodable { var days: [forecastDay] //Coding keys enum

Using Codable to instantiate a single class from 2 different JSON files without using optionals

核能气质少年 提交于 2019-12-08 01:57:06
问题 I'm working with an API that provides 2 JSON URLS. Each URL contains a nested container with different attributes that belong to the same class and object. JSON URL 1 { "last_updated": 1535936629, "xyz": 5, "data": { "dataList": [ { "id": "42", "a1": "a1value", "a2": "a2value", }, // ,,, ] } } JSON URL 2 { "last_updated": 1536639996, "xyz": 5, "data": { "dataList": [ { "id": "42", "lat": "12.345", "lon": "67.890", }, // ,,, ] } } I want to use these JSON URLS to create a single Codable

How to represent a generic JSON structure in Swift?

风格不统一 提交于 2019-12-07 07:51:23
问题 I would like to represent a generic JSON object in Swift: let foo: [String: Any] = [ "foo": 1, "bar": "baz", ] But the [String: Any] type suggested by the compiler doesn’t really work well. I can’t check two instances of the type for equality, for example, while that should be possible with two JSON trees. What also doesn’t work is using the Codable machinery to encode that value into a JSON string: let encoded = try JSONEncoder().encode(foo) Which blows up with an error: fatal error:

Make class conforming to Codable by default in Swift

与世无争的帅哥 提交于 2019-12-07 02:16:26
Such feature of Swift as Codable ( Decodable & Encodable ) protocol is very useful. But I found such issue: Let we have class Parent conforming to Codable : class Parent: Codable { var name: String var email: String? var password: String? } Ok, that class is conforming to Codable protocol "from the box", you don't need write any initializers, it's ready to be initialized from JSON like that: { "name": "John", "email": "johndoe@yahoo.com", "password": <null>} But let's say we need other class, Child inherits from Parent and be conforming to Codable: class Child: Parent { var token: String var

Using Decodable protocol with multiples keys

被刻印的时光 ゝ 提交于 2019-12-06 16:00:24
Say I have the following code: import Foundation let jsonData = """ [ {"firstname": "Tom", "lastname": "Smith", "age": {"realage": "28"}}, {"firstname": "Bob", "lastname": "Smith", "age": {"fakeage": "31"}} ] """.data(using: .utf8)! struct Person: Codable { let firstName, lastName: String let age: String? enum CodingKeys : String, CodingKey { case firstName = "firstname" case lastName = "lastname" case age } } let decoded = try JSONDecoder().decode([Person].self, from: jsonData) print(decoded) Everything is working except age is always nil . Which makes sense. My question is how can I set the

How do you design a codable JSON field which can be either an empty string or an int [closed]

怎甘沉沦 提交于 2019-12-06 11:27:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . How do you handle a field of a codable struct from JSON which can be either an empty string or an int? I tried using the data type any but that does not conform to codable. I think that if it does not have a value then it returns an empty string or otherwise, it returns an int. I am using Swift 4

Codable Error - Expected to decode Dictionary<String, Any> but found an array instead

巧了我就是萌 提交于 2019-12-06 07:59:24
I am trying to decode a JSON with the help of codable - https://pastebin.com/Xfjj2XiP However I'm getting this error when I do so. typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "txt_forecast", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil)) Here's the code I'm using: struct container: Decodable { var days: [forecastDay] //Coding keys enum CodingKeys: String, CodingKey { case forecast = "forecast" case txt_forecast = "txt_forecast" case

Swift 4 JSON decode with configurable keys

安稳与你 提交于 2019-12-06 06:51:31
问题 I'm new to Swift and I need to parse a JSON with some configurable keys . Opposite to many examples I've seen here, the keys are known before the decode operation is started, they just depend on some parameters passed to endpoint. Example: https://some.provider.com/endpoint/?param=XXX and https://some.provider.com/endpoint/?param=YYY will answer, respectively: [ { "fixed_key1": "value1", "fixed_key2": "value2", "variable_key_1_XXX": "some value", "variable_key_2_XXX": "some other value" }, ..