decodable

Swift 4 decodable with unknown dynamic keys

点点圈 提交于 2020-01-02 08:09:15
问题 I have the following JSON {"DynamicKey":6410,"Meta":{"name":"","page":""}} DynamicKey is unknown at compile time.I'm trying to find a reference how to parse this struct using decodable. public struct MyStruct: Decodable { public let unknown: Double public let meta: [String: String] private enum CodingKeys: String, CodingKey { case meta = "Meta" } } Any ideas? 回答1: To decode an arbitrary string, you need a key like this: // Arbitrary key private struct Key: CodingKey, Hashable,

Swift: Decode imprecise decimal correctly

雨燕双飞 提交于 2019-12-31 07:33:50
问题 I need to decode ( Decodable protocol) an imprecise decimal value correctly, from this question I understand how to properly handle the Decimal instantiation, but how can I do this when decoding? If trying to init any number as a String if let value = try! container.decode(String.self, forKey: .d) { self.taxAmount = Decimal(string: value) } I get Fatal Error: "Expected to decode String but found a number instead." And if try to init 130.43 as a Decimal if let value = try! container.decode

How to extract information from API using Structs and Decodable in Swift 4-5?

不羁岁月 提交于 2019-12-25 18:46:33
问题 I need to extract information from the a json using the openweather website. But I have problems on extracting the information from the "weather" (and any other) part (I need "main" and "description"). I was able to extract (so far) only the coordinates ("coord"). But I'm stuck to get the "weather" section as I mentioned above. I also tried to get the "sys" part to get the "sunrise" and "sunset" variables but I get an error. I have been capable of getting all of this information using the

How to extract information from API using Structs and Decodable in Swift 4-5?

前提是你 提交于 2019-12-25 18:46:03
问题 I need to extract information from the a json using the openweather website. But I have problems on extracting the information from the "weather" (and any other) part (I need "main" and "description"). I was able to extract (so far) only the coordinates ("coord"). But I'm stuck to get the "weather" section as I mentioned above. I also tried to get the "sys" part to get the "sunrise" and "sunset" variables but I get an error. I have been capable of getting all of this information using the

Decoding Nested JSON with Optional Values Swift 4

删除回忆录丶 提交于 2019-12-25 03:56:39
问题 I am trying to decode this JSON from this url https://jsonodds.com/api/test/odds Sometimes some of the values are nil and I wanted to avoid making all of my properties optional so I am trying to implement custom decoding. [ { "ID": "e83bf033-dbde-49ac-a073-d385ebdc66d1", "HomeTeam": "Xavier", "AwayTeam": "Marquette", "Sport": 2, "MatchTime": "2018-01-24T23:30:00", "Odds": [ { "ID": "50742eed-a1c8-4958-8678-50627ffb665e", "EventID": "e83bf033-dbde-49ac-a073-d385ebdc66d1", "MoneyLineHome": "0",

Saving array of objects in Realm with Decodable

99封情书 提交于 2019-12-25 01:18:08
问题 I've got a class which conforms to Decodable protocol (fetching data from API) and I would like to save it in the Realm database. Problem occurs when one of my properties is array (List). It says Cannot automatically synthesize Decodable because List<Item> does not conform to Decodable What is the best way to bypass this problem? Realm only supports arrays of primitive types. here is my class: class PartValue: Object, Decodable { @objc dynamic var idetifier: Int = 0 let items = List<Item>() }

Swift 4 Decodable multiple containers

三世轮回 提交于 2019-12-24 07:07:35
问题 I'm trying to understand how could I parse this multiple container JSON to an object. I've tried this approach (Mark answer), but he explain how to solve it using one-level container. For some reason I can't mimic the behaviour for multiple containers. { "graphql": { "shortcode_media": { "id": "1657677004214306744", "shortcode": "BcBQHPchwe4" } } } class Post: Decodable { enum CodingKeys: String, CodingKey { case graphql // The top level "user" key case shortcode_media } enum PostKeys: String

Swift 4 decodable nested json with random key attributes

时光毁灭记忆、已成空白 提交于 2019-12-21 20:43:08
问题 I'm having problems decoding json. I've followed lots of tutorials but non use complex json structures. For simplicity I minimized the code and use Dog as example. In following json i'm mostly only interested in the Dog structs. The json "Data" attribute contains random dog names . So I cannot use coding keys because I dont know the attribute name. { "Response": "success" "BaseLinkUrl": "https://wwww.example.com", "Data": { "Max": { "name": "Max", "breed": "Labrador" }, "Rocky": { "name":

Decodable and JSON, 2 datatypes for same variable

纵饮孤独 提交于 2019-12-21 06:04:46
问题 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

Swift Codable null handling

烈酒焚心 提交于 2019-12-21 04:22:20
问题 I have a struct that parse JSON using Codable . struct Student: Codable { let name: String? let amount: Double? let adress: String? } Now if the amount value is coming as null the JSON parsing is failing. So should I manually handle the null cases for all the Int and Double that are present in the Student struct? The String values coming as null is automatically handled. 回答1: Let me do this Playground for you since an example shows you more than a hundred words: import Cocoa struct Student: