decodable

Swift Codable null handling

浪尽此生 提交于 2019-12-03 14:41:57
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. Let me do this Playground for you since an example shows you more than a hundred words: import Cocoa struct Student: Codable { let name: String? let amount: Double? let adress: String? } let okData = """ { "name": "here",

How to write a Decodable for a JSON in Swift 4, where keys are dynamic?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 13:50:49
问题 I've a JSON like this. I need to make a corresponding Decodable struct in my iOS app using Swift 4. { "cherry": { "filling": "cherries and love", "goodWithIceCream": true, "madeBy": "my grandmother" }, "odd": { "filling": "rocks, I think?", "goodWithIceCream": false, "madeBy": "a child, maybe?" }, "super-chocolate": { "flavor": "german chocolate with chocolate shavings", "forABirthday": false, "madeBy": "the charming bakery up the street" } } Need help on making the Decodable Struct. How to

Codable class does not conform to protocol Decodable

不羁岁月 提交于 2019-12-03 06:30:08
问题 Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { case indexPath case locationInText } init(publication: Publication?, indexPath: [Int]) { self.publication = publication self.indexPath = indexPath } } I do not wish to save the publication var since the Publication owns the Bookmark but the bookmark needs to

How to reference a generic Decodable struct in Swift 4

冷暖自知 提交于 2019-12-03 03:58:09
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.data!) { //success } } and instead of saying "MyDecodableStruct", I would like it to be any Decodable

How to write a Decodable for a JSON in Swift 4, where keys are dynamic?

烂漫一生 提交于 2019-12-03 03:52:00
I've a JSON like this. I need to make a corresponding Decodable struct in my iOS app using Swift 4. { "cherry": { "filling": "cherries and love", "goodWithIceCream": true, "madeBy": "my grandmother" }, "odd": { "filling": "rocks, I think?", "goodWithIceCream": false, "madeBy": "a child, maybe?" }, "super-chocolate": { "flavor": "german chocolate with chocolate shavings", "forABirthday": false, "madeBy": "the charming bakery up the street" } } Need help on making the Decodable Struct. How to mention the unknown keys like cherry , odd and super-chocolate . What you need is to get creative in

Codable class does not conform to protocol Decodable

六月ゝ 毕业季﹏ 提交于 2019-12-02 21:43:48
Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { case indexPath case locationInText } init(publication: Publication?, indexPath: [Int]) { self.publication = publication self.indexPath = indexPath } } I do not wish to save the publication var since the Publication owns the Bookmark but the bookmark needs to know which Publication it belongs to. The decode init of Publication will set the bookmark reference to

Decoding dynamic JSON structure in swift 4

十年热恋 提交于 2019-12-02 15:42:54
问题 I have the following issue that I'm not sure how to handle. My JSON response can look like this: { "data": { "id": 7, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDY1MTU0NDMsImRhdGEiOiJ2bGFkVGVzdCIsImlhdCI6MTU0NjUwODI0M30.uwuPhlnchgBG4E8IvHvK4bB1Yj-TNDgmi7wUAiKmoVo" }, "error": null } Or like this: { "data": [{ "id": 12 }, { "id": 2 }, { "id": 5 }, { "id": 7 }], "error": null } So in short the data can be either a single objet or an Array. What i have is this: struct ApiData:

Decoding dynamic JSON structure in swift 4

孤街浪徒 提交于 2019-12-02 12:03:18
I have the following issue that I'm not sure how to handle. My JSON response can look like this: { "data": { "id": 7, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDY1MTU0NDMsImRhdGEiOiJ2bGFkVGVzdCIsImlhdCI6MTU0NjUwODI0M30.uwuPhlnchgBG4E8IvHvK4bB1Yj-TNDgmi7wUAiKmoVo" }, "error": null } Or like this: { "data": [{ "id": 12 }, { "id": 2 }, { "id": 5 }, { "id": 7 }], "error": null } So in short the data can be either a single objet or an Array. What i have is this: struct ApiData: Decodable { var data: DataObject? var error: String? } struct DataObject: Decodable { var userId: Int?

How to parse one block of JSON data instead of the entire JSON sequence

北城余情 提交于 2019-12-02 10:07:16
Below is the code that am using to parse a JSON api. The code works and i am able to parse all the JSON values into the struct below but i would like to know how to parse only the first block(?)/ array and store them in the struct so that i can perform operations on them. let jsonUrlString = "https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=EUR&to_symbol=USD&interval=5min&apikey=demo" let urlObj = URL(string: jsonUrlString) URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in guard let data = data else { return } do { let forex = try JSONDecoder().decode(Root

Implement Codable for an Array of Dictionary with failable responses

为君一笑 提交于 2019-12-02 09:37:56
问题 My JSON response is the following: { "data": [ { "unknown-key-c3e7f0": { "date_time": 1546944854000, "medication": "f4f25ea4-0607-4aac-b85a-edf40cc7d5b6", "record": { "status": "never" } }, "unknown-key-619d40": { "date_time": 1546944854000, "medication": "deef2278-f176-418f-ac34-c65fa54e712c", "record": { "status": "always" } }, "event": "06b445b9-dae0-48a1-85e4-b9f48c9a2349", "created": 1546949155020, "user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a", "id": "1546944855000", "type": "compliance