decodable

How to decode a JSON property with different types? [duplicate]

只愿长相守 提交于 2019-11-29 08:37:45
This question already has an answer here: What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable? 1 answer I have a JSON { "tvShow": { "id": 5348, "name": "Supernatural", "permalink": "supernatural", "url": "http://www.episodate.com/tv-show/supernatural", "description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series

How to parse JSON with Decodable protocol when property types might change from Int to String?

你离开我真会死。 提交于 2019-11-28 14:43:06
I have to decode a JSON with a big structure and a lot of nested arrays. I have reproduced the structure in my UserModel file, and it works, except with one property (postcode) that is in a nested array (Location) that sometimes is an Int and some other is a String. I don't know how to handle this situation and tried a lot of different solutions. The last one I've tried is from this blog https://agostini.tech/2017/11/12/swift-4-codable-in-real-life-part-2/ And it suggests using generics. But now I can't initialize the Location object without providing a Decoder(): Any help or any different

Implementing Codable for UIColor

限于喜欢 提交于 2019-11-28 13:10:52
Is it possible to implement the Encodable and Decodable properties for UIColor When I try to add a Decodable extension I get an error extension UIColor : Decodable { public required init(from decoder: Decoder) throws { self.init(red: 1, green: 1, blue: 1, alpha: 1) } } error: ColorStuff.playground:98:21: error: initializer requirement 'init(from:)' can only be satisfied by a required initializer in the definition of non-final class 'UIColor' public required init(from decoder: Decoder) throws { Am I missing something obvious here? I have no issues with the Encodable extension - it seems its a

Decoding JSON array of different types in Swift

夙愿已清 提交于 2019-11-28 06:13:28
问题 I'm trying to decode the following JSON Object { "result":[ { "rank":12, "user":{ "name":"bob","age":12 } }, { "1":[ { "name":"bob","age":12 }, { "name":"tim","age":13 }, { "name":"tony","age":12 }, { "name":"greg","age":13 } ] } ] } struct userObject { var name: String var age: Int } Basically a JSON Array with two different object types { "rank":12, "user": {userObject} } and a "1" : array of [userObjects] struct data: Decodable { rank: Int user: user 1: [user] <-- this is one area Im stuck

Flattening JSON when keys are known only at runtime

一笑奈何 提交于 2019-11-28 02:21:25
问题 Let's say we have a JSON structure like the following (commonly used in Firebase's Realtime Database): { "18348b9b-9a49-4e04-ac35-37e38a8db1e2": { "isActive": false, "age": 29, "company": "BALOOBA" }, "20aca96e-663a-493c-8e9b-cb7b8272f817": { "isActive": false, "age": 39, "company": "QUONATA" }, "bd0c389b-2736-481a-9cf0-170600d36b6d": { "isActive": false, "age": 35, "company": "EARTHMARK" } } Expected solution: Using Decodable I'd like to convert it into an array of 3 elements: struct

How to decode a JSON property with different types? [duplicate]

亡梦爱人 提交于 2019-11-28 01:57:04
问题 This question already has an answer here : What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable? (1 answer) Closed last year . I have a JSON { "tvShow": { "id": 5348, "name": "Supernatural", "permalink": "supernatural", "url": "http://www.episodate.com/tv-show/supernatural", "description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became

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

人盡茶涼 提交于 2019-11-27 12:25:51
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 have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call. /// /// - parameter type: The type of value to decode. /// - parameter key: The key that the decoded value is associated with. /// - returns: A decoded value of the requested type, or `nil` if the

How to use Any in Codable Type

喜夏-厌秋 提交于 2019-11-27 08:39:43
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 Codable . What I need to know is how can I make it work. Codable needs to know the type to cast to. Firstly I would try to address the issue of not knowing the type, see if you can fix that and make it simpler. Otherwise the only way I can think of solving your issue currently is to use generics like below. struct Person<T> { var id: T var name: String } let

Implementing Codable for UIColor

半世苍凉 提交于 2019-11-27 07:31:58
问题 Is it possible to implement the Encodable and Decodable properties for UIColor When I try to add a Decodable extension I get an error extension UIColor : Decodable { public required init(from decoder: Decoder) throws { self.init(red: 1, green: 1, blue: 1, alpha: 1) } } error: ColorStuff.playground:98:21: error: initializer requirement 'init(from:)' can only be satisfied by a required initializer in the definition of non-final class 'UIColor' public required init(from decoder: Decoder) throws

Swift 4 JSON Decodable with multidimensional and multitype array

僤鯓⒐⒋嵵緔 提交于 2019-11-27 05:32:46
{ "values":[ [1,1,7,"Azuan Child","Anak Azuan","12345","ACTIVE","Morning",7,12,"2017-11-09 19:45:00"], [28,1,0,"Azuan Child2","Amran","123456","ACTIVE","Evening",1,29,"2017-11-09 19:45:00"] ] } Ok, this is my json format that i received from the server Right now i want to decode it into my struct but still have no luck on it. struct ChildrenTable: Decodable { var values: [[String]]? } And my caller method on URLSession look like this URLSession.shared.dataTask(with: request) { (data, response, err) in guard let data = data else { return } let dataAsString = String(data: data, encoding: .utf8)