decodable

Decode PropertyList using Swift 4 Codable PropertyListDecoder()

微笑、不失礼 提交于 2019-12-12 13:29:21
问题 Im trying to decode a plist using PropertyListDecoder() but when I get to the trying to access the keys, I get an error that says it's in the wrong format. I'm at a loss on what I'm doing wrong. Im under the assumption I can decode a Plist file the same way I can decode a JSON file right? I don't know, I'm still new to this. //struct for PLists struct AccessControl: Decodable { enum AccessControlKeys: String, CodingKey { case api } enum KeySecretKeys: String, CodingKey { case apiKey = "KEY"

Codable : does not conform to protocol 'Decodable'

坚强是说给别人听的谎言 提交于 2019-12-12 10:53:55
问题 Not able to figure why my class does not conform to Codable Please not that in my case I do not need to implement the methods encode and decode . public class LCLAdvantagePlusJackpotCache: Codable { public let token: String public let amount: NSNumber public let member: Bool public init(token: String, amount: NSNumber, member: Bool) { self.token = token self.amount = amount self.member = member } enum CodingKeys: String, CodingKey { case token, amount, member } } 回答1: It's because NSNumber is

Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable

↘锁芯ラ 提交于 2019-12-12 07:28:21
问题 I'm trying to use swift 4 to parse a local json file: { "success": true, "lastId": null, "hasMore": false, "foundEndpoint": "https://endpoint", "error": null } This is the function I'm using: func loadLocalJSON() { if let path = Bundle.main.path(forResource: "localJSON", ofType: "json") { let url = URL(fileURLWithPath: path) do { let data = try Data(contentsOf: url) let colors = try JSONDecoder().decode([String: Any].self, from: data) print(colors) } catch { print("Local JSON not loaded")} }

json swift4 how to set the struct?

[亡魂溺海] 提交于 2019-12-11 06:17:16
问题 I want to parse a json from a http resource (it's my router so http is mandatory). After I set the Info.plist App Security Transport that I do get a connection I get the data by 1st Attempted: let sphDataAddress = "http://speedport.ip/data/status.json" let url = URL(string: sphDataAddress)! let jsonData = try! Data(contentsOf: url) // ! is just for testing reason and will be in real app by guard let print("data \(jsonData)") // shows that the received Data are 5077bytes struct User { let

Subclassing swift generic decodable type

允我心安 提交于 2019-12-11 04:06:08
问题 EDIT: As Rob Napier wrote, the problem exists in Xcode 9.2. In Xcode 9.3 the issue is no longer relevant. My server json responses are all packed inside data object: { "data": {...} } So I have the following generic type to parse JSON: class DataContainer<T: Decodable>: Decodable { let data: T init(data: T) self.data = data } } Most of the time it works fine, but there is one response in which I also need to parse included field so I have created SpecificDataContainer subclass: class

Decodable nested data without creating additional class in Swift

夙愿已清 提交于 2019-12-10 19:57:31
问题 I'm new in iOS development, so sorry for stupid question in advance. I have json like this: { "type":"post", "comments":{ "count":0, "can_post":1 }, "likes":{ "count":0, "user_likes":0, "can_like":1, "can_publish":1 }, "reposts":{ "count":0, "user_reposted":0 } } I want to convert this to class which will contain just likesCount, commentsCount, repostsCount but without creating separate classes for comments , likes , reposts . I'm using Decodable for this and here is my code which doesn't

How to reference a generic Decodable struct in Swift 4

旧巷老猫 提交于 2019-12-09 04:46:29
问题 I have a function that I would like to reuse and have it accept a parameter of a Decocable struct. For example, this is a simplification of my current code (assume "MyDecodableStruct" is a Decodable struct declared elsewhere in the app): static func getResults(url: String, parameters: Parameters) { // On success REST response if response.result.isSuccess { struct Results: Decodable { let items: [MyDecodableStruct] } if let jsonResults = try? JSONDecoder().decode(Results.self, from: response

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

孤者浪人 提交于 2019-12-08 02:04:02
问题 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

Using Decodable with inheritance raises an exception

你离开我真会死。 提交于 2019-12-07 05:51:13
问题 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

Decoding Void with Swift 4’s Decodable

怎甘沉沦 提交于 2019-12-06 17:45:36
问题 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