codable

How to Decode JSON in Swift 4?

五迷三道 提交于 2020-01-06 06:47:06
问题 How to decode following json using Swift 4? { "data": { "id": 22, "packageId": 5, "Package": { "id": 5, "color": "blue" } }, "error": false, "message": "Successfully Fetched" } I have tried it using following: struct Root: Codable { enum CodingKeys: String, CodingKey { case id = "id" case packageId = "packageId" case package = "Package" } var package : Package var id : Int var packageId : Int } struct Package : Codable { var id : Int var color : String } It is giving me following error:

Using KeyedDecodingContainer to decode an object with a random key

我是研究僧i 提交于 2020-01-06 06:45:48
问题 I have an array of objects that looks like this that I want to decode let accountPending = """ { "blocks": { "F143CCC051927EEF59EEA78D16D80F855052BBF159EA6602843904C9445": { "amount": "10000000000000000000000000000000", "source": "6xswkroybxydyzaxybb1h531sx34omiu7an9t9jy19f9mca7a36s7by5e" }, } } """.data(using: .utf8)! So I'm trying something along these lines struct PendingBlock: Decodable { let work: [String: PendingBlockData] enum CodingKeys: String, CodingKey { case work = "???" } init

How to keep a flexible structure when using Codable in Swift

百般思念 提交于 2020-01-06 04:30:06
问题 I've got a API response structure, representing a user object, that looks like this: { "statuscode": 200, "response_type": 3, "errormessage": null, "detailresponse": { "id": "2", "shopifyautosync": null, "platformfeepercentage": null, "invited": null, "requiresyoutubesocialmediaupdate": 1, // Other properties ... } I'm using JSONDecoder().decode to decode to following structures: import Foundation class Response: Decodable { var statuscode: Int? var response_type: Int? // Other properties var

How to decode variable from json when key is changing according to user input?

放肆的年华 提交于 2020-01-06 03:43:05
问题 I am trying to parse some JSON response coming from CoinmarketCap using the JSONDecoder() in Swift 4. But the problem is that the response from json is changing according to user input. e.g if user wants the price in eur, the output is following: [ { "price_eur": "9022.9695444" } ] but if user wants the price in gbp: [ { "price_gbp": "7906.8032145" } ] So the question is how should I make the struct that inherits from Decodable if the variable(json key) name is changing? 回答1: You can decode

Why isn't the Codable init being called instead of the other required init?

自闭症网瘾萝莉.ら 提交于 2020-01-05 06:46:30
问题 I am building a game using SpriteKit and Swift 4, with a number of custom classes in the SKNode family. I am trying to use the Codable protocol with them to (hypothetically) make saving the game easier. As a bit of context, here is a high level overview of the main classes in the game: Hero class, custom SKSpriteNode, implements Codable. The Hero class, as a property, has a dictionary in the form of: [String:MapNode] MapNode class, custom SKSpriteNode, implements Codable. The MapNode class,

Swift 4 Codable Realm Object Subclasses

大城市里の小女人 提交于 2020-01-05 06:38:10
问题 trying to switch some of my codebase over to Swift 4's new nifty Codable protocol. My setup looks something like this: class Base: Object, Codable { dynamic var id: String = "" dynamic var timestamp: String = "" private enum CodingKeys: String, CodingKey { case id = "_id" case timestamp = "timestamp" } } class User: Base { dynamic var name: String = "" private enum CodingKeys: String, CodingKey { case name = "name" } required init(from decoder: Decoder) throws { let container = try decoder

Swift 4 Codable - Bool or String values

非 Y 不嫁゛ 提交于 2019-12-29 01:26:09
问题 Looking for some input as to how you would handle the scenario I recently ran into. I have been using Swift 4s Codable with success but today noticed a crash that I didn't expect. The API that I am working with, says it returns a boolean for the key manage_stock . My stubbed struct looks like: struct Product: Codable { var manage_stock: Bool? } That works fine, the problem is that the API sometimes returns a string instead of a boolean . Because of this, my decode fails with: Expected to

Swift 4 Decodable - Dictionary with enum as key

为君一笑 提交于 2019-12-28 05:19:06
问题 My data structure has an enum as a key, I would expect the below to decode automatically. Is this a bug or some configuration issue? import Foundation enum AnEnum: String, Codable { case enumValue } struct AStruct: Codable { let dictionary: [AnEnum: String] } let jsonDict = ["dictionary": ["enumValue": "someString"]] let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted) let decoder = JSONDecoder() do { try decoder.decode(AStruct.self, from: data) } catch {

With JSONDecoder in Swift 4, can missing keys use a default value instead of having to be optional properties?

牧云@^-^@ 提交于 2019-12-27 11:53:53
问题 Swift 4 added the new Codable protocol. When I use JSONDecoder it seems to require all the non-optional properties of my Codable class to have keys in the JSON or it throws an error. Making every property of my class optional seems like an unnecessary hassle since what I really want is to use the value in the json or a default value. (I don't want the property to be nil.) Is there a way to do this? class MyCodable: Codable { var name: String = "Default Appleseed" } func load(input: String) {

How to decode a nested JSON struct with Swift Decodable protocol?

天涯浪子 提交于 2019-12-27 10:42:47
问题 Here is my JSON { "id": 1, "user": { "user_name": "Tester", "real_info": { "full_name":"Jon Doe" } }, "reviews_count": [ { "count": 4 } ] } Here is the structure I want it saved to (incomplete) struct ServerResponse: Decodable { var id: String var username: String var fullName: String var reviewCount: Int enum CodingKeys: String, CodingKey { case id, // How do i get nested values? } } I have looked at Apple's Documentation on decoding nested structs, but I still do not understand how to do