codable

Protocol extending Encodable (or Codable) does not conform to it

流过昼夜 提交于 2019-12-01 20:58:17
I have 2 protocols, Filters and Parameters , both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var filters: Filters { get } } I create structs conforming to these protocols, thusly… struct BankAccountFilters: Filters { var page: Int var isWithdrawal: Bool } struct BankAccountParamters: Parameters { let type: String = "Bank" var filters: Filters } let baf = BankAccountFilters(page: 1, isWithdrawal: true) let bap = BankAccountParamters(filters: baf) Which fails because… error: type 'BankAccountParamters'

How to extend float3 or any other built-in type to conform to the Codable protocol?

两盒软妹~` 提交于 2019-12-01 20:06:13
In trying to serialize an array of float3 objects with the basic JSONEncoder, it's revealed that float3 does not conform to the Codable protocol, so this cannot be done. I tried to write a basic extension as suggested in Encoding and Decoding Custom Types as seen below, but the error 'self' used before all stored properties are initialized is rendered for each of the assignment lines in the init. I assume this is because the compiler is not sure that Float.self is defined prior to the initialization of float3, but I'm not sure how to resolve this. Further, the end of the init says Return from

Codable/Decodable should decode Array with Strings

一笑奈何 提交于 2019-12-01 18:48:56
Why is the names Array not decoding? Prepared for Playground, Simple paste this into your playground import Foundation struct Country : Decodable { enum CodingKeys : String, CodingKey { case names } var names : [String]? } extension Country { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) names = try values.decode([String]?.self, forKey: .names)! } } let json = """ [{ "names": [ "Andorre", "Andorra", "アンドラ" ] },{ "names": [ "United Arab Emirates", "Vereinigte Arabische Emirate", "Émirats Arabes Unis", "Emiratos Árabes Unidos", "アラブ首長国連邦

How to parse JSON using swift 4

纵饮孤独 提交于 2019-12-01 14:48:00
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.JSONData.com/fruits") URLSession.shared.dataTask(with: url!) { (data, response, error) in guard let data =

Cannot invoke 'decode' with an argument list of type '(T, from: Data)'

a 夏天 提交于 2019-12-01 10:30:35
I'm trying to create a function that takes in a parameter of type 'Codable' based on the custom JSON models being passed to it. The error : Cannot invoke 'decode' with an argument list of type '(T, from: Data)' happens on the decode line, here is the function: static func updateDataModels <T : Codable> (url: serverUrl, type: T, completionHandler:@escaping (_ details: Codable?) -> Void) { guard let url = URL(string: url.rawValue) else { return } URLSession.shared.dataTask(with: url) { (data, response, err) in guard let data = data else { return } do { let dataFamilies = try JSONDecoder().decode

Swift Codable with Different Array Types

那年仲夏 提交于 2019-12-01 08:28:10
I'm writing a program where I'm parsing JSON data that includes array of arrays, where the nested arrays have different object types (specifically, [[String, String, Int]]). For example, { "number": 5295, "bets": [ [ "16", "83", 9 ], [ "75", "99", 4 ], [ "46", "27", 5 ] ] } I'm trying to use codable to help me parse the data, but when I try something like struct OrderBook: Codable { let number: Int let bets: [Bet] } struct Bet: Codable { let price: String let sale: String let quantity: Int } it gives me errors saying that Expected to decode Dictionary <String, Any> but found an array instead

Cannot invoke 'decode' with an argument list of type '(T, from: Data)'

。_饼干妹妹 提交于 2019-12-01 07:29:40
问题 I'm trying to create a function that takes in a parameter of type 'Codable' based on the custom JSON models being passed to it. The error : Cannot invoke 'decode' with an argument list of type '(T, from: Data)' happens on the decode line, here is the function: static func updateDataModels <T : Codable> (url: serverUrl, type: T, completionHandler:@escaping (_ details: Codable?) -> Void) { guard let url = URL(string: url.rawValue) else { return } URLSession.shared.dataTask(with: url) { (data,

Swift Codable with Different Array Types

霸气de小男生 提交于 2019-12-01 06:04:16
问题 I'm writing a program where I'm parsing JSON data that includes array of arrays, where the nested arrays have different object types (specifically, [[String, String, Int]]). For example, { "number": 5295, "bets": [ [ "16", "83", 9 ], [ "75", "99", 4 ], [ "46", "27", 5 ] ] } I'm trying to use codable to help me parse the data, but when I try something like struct OrderBook: Codable { let number: Int let bets: [Bet] } struct Bet: Codable { let price: String let sale: String let quantity: Int }

How to make NSAttributedString codable compliant?

纵然是瞬间 提交于 2019-12-01 04:19:53
问题 What is the problem? Currently I am building an app-extension on my main app which communicates via a JSON. Theming and data is located in the JSON and is being parsed via the codable protocol from Apple. The problem I am experiencing right now is making NSAttributedString codable compliant. I know it is not build in but I know it can be converted to data and back to an nsattributedstring. What I have so far? Cast a NSAttributedString to data in order to share it via a JSON. if let

Parsing Nested JSON in SWIFT 4

前提是你 提交于 2019-12-01 04:15:05
问题 I am currently parsing JSON with Decodable in SWIFT 4. The JSON is formatted as follows: { "autopayout_from": "1.010", "earning_24_hours": "9.74731104", "error": false, "immature_earning": 0.635593030875, "last_payment_amount": "1.91238210", "last_payment_date": "Mon, 26 Feb 2018 15:08:02 GMT", "last_share_date": "Mon, 26 Feb 2018 16:16:01 GMT", "payout_daily": false, "payout_request": false, "total_hashrate": 109006.86, "total_hashrate_calculated": 143855.75, "transferring_to_balance": 0