codable

Swift: Codable - extract a single coding key

帅比萌擦擦* 提交于 2020-05-10 14:20:33
问题 I have the following code to extract a JSON contained within a coding key: let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"]) This successfully handles the following JSONs: { "applmusic":{ "code":"AAPL", "quality":"good", "line":"She told me don't worry", } However, fails to extract a JSON with the coding key of applmusic from the following one: { "applmusic":{ "code":"AAPL", "quality":"good", "line":"She told me don't worry", }, "spotify":{ "differentcode":"SPOT"

iOS Generic type for codable property in Swift

独自空忆成欢 提交于 2020-04-08 01:48:35
问题 I need to get a generic variable for a struct for parsing a JSON but there is an error that I am getting Type 'BaseJsonModel' does not conform to protocol 'Codable Below is my struct struct BaseJsonStruct<T>: Codable { let info: String let data: T } Error:- Type 'BaseJsonModel' does not conform to protocol 'Codable' 回答1: T must also conform to Codable struct BaseJsonStruct<T : Codable> : Codable { let info: String let data: T } 来源: https://stackoverflow.com/questions/49529208/ios-generic-type

iOS Generic type for codable property in Swift

元气小坏坏 提交于 2020-04-08 01:38:16
问题 I need to get a generic variable for a struct for parsing a JSON but there is an error that I am getting Type 'BaseJsonModel' does not conform to protocol 'Codable Below is my struct struct BaseJsonStruct<T>: Codable { let info: String let data: T } Error:- Type 'BaseJsonModel' does not conform to protocol 'Codable' 回答1: T must also conform to Codable struct BaseJsonStruct<T : Codable> : Codable { let info: String let data: T } 来源: https://stackoverflow.com/questions/49529208/ios-generic-type

iOS Generic type for codable property in Swift

时光毁灭记忆、已成空白 提交于 2020-04-08 01:36:46
问题 I need to get a generic variable for a struct for parsing a JSON but there is an error that I am getting Type 'BaseJsonModel' does not conform to protocol 'Codable Below is my struct struct BaseJsonStruct<T>: Codable { let info: String let data: T } Error:- Type 'BaseJsonModel' does not conform to protocol 'Codable' 回答1: T must also conform to Codable struct BaseJsonStruct<T : Codable> : Codable { let info: String let data: T } 来源: https://stackoverflow.com/questions/49529208/ios-generic-type

How to parse JSON using swift 4

ε祈祈猫儿з 提交于 2020-03-11 15:10:44
问题 I am confusing to getting detail of fruit { "fruits": [ { "id": "1", "image": "https://cdn1.medicalnewstoday.com/content/images/headlines/271/271157/bananas.jpg", "name": "Banana" }, { "id": "2", "image": "http://soappotions.com/wp-content/uploads/2017/10/orange.jpg", "title": "Orange" } ] } Want to parse JSON using "Decodable" struct Fruits: Decodable { let Fruits: [fruit] } struct fruit: Decodable { let id: Int? let image: String? let name: String? } let url = URL(string: "https://www

How to resolve error in unit testing when we have Date comparison in Codable

泪湿孤枕 提交于 2020-02-29 09:22:36
问题 I tried to use custom date strategy in json decoding using Codable as describe in https://stackoverflow.com/a/28016692/3477974 . Here is the simplified implementation code: public extension Formatter { static let iso8601: DateFormatter = { let formatter = DateFormatter() formatter.calendar = Calendar(identifier: .iso8601) formatter.locale = Locale(identifier: "en_US_POSIX") formatter.timeZone = TimeZone(secondsFromGMT: 0) formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" return

Decoding/Encoding a struct with protocol type properties

谁说胖子不能爱 提交于 2020-02-06 08:08:14
问题 I am trying to save a configuration data structure with UserDefaults , thus the data structure needs to conform to the Codable protocol. This is my data structure: // Data structure which saves two objects, which conform to the Connection protocol struct Configuration { var from: Connection var to: Connection } protocol Connection: Codable { var path: String { get set } } // Two implementations of the Connection protocol struct SFTPConnection: Connection, Codable { var path: String var user:

Swift Decodable, Endpoint returns completely different types

人盡茶涼 提交于 2020-02-06 08:03:56
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Swift Decodable, Endpoint returns completely different types

不想你离开。 提交于 2020-02-06 08:03:25
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Fail to use Protocol contains Protocol adopting Codable

泄露秘密 提交于 2020-02-04 05:48:05
问题 Consider the followings: protocol A: Codable { var b: B { get } var num: Int { get } } protocol B: Codable { var text: String { get } } struct C: A { var b: B var num: Int } The compiler gives two errors Type 'C' does not conform to protocol 'Decodable' Type 'C' does not conform to protocol 'Encodable' However Both A and B are Codable. How to solve/avoid these errors? Reference: EDITED As the auto-synthesis for Codable not working, I manually implemented the required methods. struct C: A {