decodable

Dictionary of String:Any does not conform to protocol 'Decodable' [duplicate]

狂风中的少年 提交于 2019-12-06 07:39:17
This question already has answers here : Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable (2 answers) Closed last year . I'm trying to implement a Decodable to parse a json request but the json request has a dictionary inside of the object. Here is my code: struct myStruct : Decodable { let content: [String: Any] } enum CodingKeys: String, CodingKey { case content = "content" } But I'm getting this error: Type 'MyClass.myStruct' does not conform to protocol 'Decodable' How can declare a variable as dictionary without this error? I'll

Swift Codable init

不羁的心 提交于 2019-12-06 02:52:16
问题 I would like to do some initialization logic after the Swift Coding/Encoding feature has finished decoding a JSON. struct MyStruct: Codable { let id: Int var name: String init() { name = "\(id) \(name)" } } But I get the compiler error: Return from initializer without initializing all stored properties Which is clear to me because init() wants me to initialise all properties. But adding an init() with all needed properties also doesn't solve it because this initializer is not called(!) when

Extracting data from JSON array with swift Codable

懵懂的女人 提交于 2019-12-06 01:38:45
I have a JSON response like this: I have currently designed my decodable struct to be as follows: struct PortfolioResponseModel: Decodable { var dataset: Dataset struct Dataset: Decodable { var data: Array<PortfolioData> //I cannot use [Any] here... struct PortfolioData: Decodable { //how to extract this data ? } } } The question is, how do I extract the data inside the array, which can have a value Double or String. Here is the sample string to make this work on playground: let myJSONArray = """ { "dataset": { "data": [ [ "2018-01-19", 181.29 ], [ "2018-01-18", 179.8 ], [ "2018-01-17", 177.6

Using Decodable with inheritance raises an exception

纵饮孤独 提交于 2019-12-05 10:41:39
I'm working against Rest API service, where the responses are divided into Base response, and all other responses inherit from it. I'm trying to building the same structure for my response model classes, using the Decoder interface. However i'm having issues with the decoding of an inherited class. I tried to follow this issue: Using Decodable in Swift 4 with Inheritance But with no luck. This is the initial structure: class LoginResponse: BaseResponse{ var Message: String? private enum CodingKeys: String, CodingKey{ case Message } required init(from decoder: Decoder) throws { let container =

Decoding Void with Swift 4’s Decodable

人走茶凉 提交于 2019-12-05 00:46:24
I have a generic REST request: struct Request<T> {…} The T is the return type of the request, for example: struct Animal {…} let animalRequest = Request<Animal> let animal: Animal = sendRequest(animalRequest) Now I would like to express that the generic type has to conform to Decodable so that I can decode the JSON response from the server: struct Request<T> where T: Decodable {…} struct Animal: Decodable {…} This makes sense and works – until I arrive at a request that has no response, a Request<Void> . The compiler is not happy about that one: Type 'Void' does not conform to protocol

Swift 4 Decodable: struct from nested array

穿精又带淫゛_ 提交于 2019-12-04 23:07:17
问题 Given the following JSON document I'd like to create a struct with four properties: filmCount (Int), year (Int), category (String), and actor (Actor array). { "filmCount": 5, "year": 2018, "category": "Other", "actors":{ "nodes":[ { "actor":{ "id":0, "name":"Daniel Craig" } }, { "actor":{ "id":1, "name":"Naomie Harris" } }, { "actor":{ "id":2, "name":"Rowan Atkinson" } } ] } } PlacerholderData is a struct storing the three main properties and the list of actors which should be retrieved from

Swift Codable init

懵懂的女人 提交于 2019-12-04 07:16:45
I would like to do some initialization logic after the Swift Coding/Encoding feature has finished decoding a JSON. struct MyStruct: Codable { let id: Int var name: String init() { name = "\(id) \(name)" } } But I get the compiler error: Return from initializer without initializing all stored properties Which is clear to me because init() wants me to initialise all properties. But adding an init() with all needed properties also doesn't solve it because this initializer is not called(!) when Codable kicks in: init(id: Int, name: String) { // This initializer is not called if Decoded from JSON!

Codable/Decodable should decode Array with Strings

蹲街弑〆低调 提交于 2019-12-04 03:50:13
问题 Why is the names Array not decoding? Prepared for Playground, Simple paste this into your playground import Foundation struct Country : Decodable { enum CodingKeys : String, CodingKey { case names } var names : [String]? } extension Country { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) names = try values.decode([String]?.self, forKey: .names)! } } let json = """ [{ "names": [ "Andorre", "Andorra", "アンドラ" ] },{ "names": [ "United

Decodable and JSON, 2 datatypes for same variable

蓝咒 提交于 2019-12-03 21:43:12
I'm using the Decodable protocol to decode some json, but I've run into a problem: I'm getting an answer back, where a longitude and a latitide can be either an interger (latitude = 0) if there's no geo location data added to the element, and a String (fx. latitude = "25.047880") if there's geodata available. Now when I decode the json, I don't know how to build my Struct, as the long and lat can't both be String and Int.. So I'm getting a decode error when fetching elements where both cases are represented. Any suggestions about how to solve this? I've tried with "Any" as datatype, but this

Swift 4 Decodable: struct from nested array

本小妞迷上赌 提交于 2019-12-03 16:11:45
Given the following JSON document I'd like to create a struct with four properties: filmCount (Int), year (Int), category (String), and actor (Actor array). { "filmCount": 5, "year": 2018, "category": "Other", "actors":{ "nodes":[ { "actor":{ "id":0, "name":"Daniel Craig" } }, { "actor":{ "id":1, "name":"Naomie Harris" } }, { "actor":{ "id":2, "name":"Rowan Atkinson" } } ] } } PlacerholderData is a struct storing the three main properties and the list of actors which should be retrieved from the nested nodes container within the actors property from the JSON object. PlacerholderData: struct