codable

How to make Swift Codable types more versatile

╄→尐↘猪︶ㄣ 提交于 2020-07-19 04:27:49
问题 I currently am working with an API that deals with bus predictions. There is an interesting quirk with the JSON that is returned for the predictions of a certain stop. When there are multiple predictions for a stop, the JSON looks something like this: ... "direction": { "prediction": [ { "affectedByLayover": "true", "block": "241", "dirTag": "loop", "epochTime": "1571785998536", "isDeparture": "false", "minutes": "20", "seconds": "1208", "tripTag": "121", "vehicle": "1698" }, {

Inheritance of Encodable Class

时间秒杀一切 提交于 2020-07-18 09:05:20
问题 I am writing a program using Swift 4 and Xcode 9.2. I have faced difficulties with writing encodable class (exactly class, not struct). When I am trying to inherit one class from another, JSONEncoder does not take all properties from sub class (child). Please look at this: class BasicData: Encodable { let a: String let b: String init() { a = "a" b = "b" } } class AdditionalData: BasicData { let c: String init(c: String) { self.c = c } } let encode = AdditionalData(c: "c") do { let data = try

How can I decode partially double serialized json string using `Codable` protocol?

爱⌒轻易说出口 提交于 2020-07-16 05:07:36
问题 How can I decode partially double serialized json string using Codable protocol? class Person : Codable { var name : String? var hobby : String? } class Family : Codable { var person: String? var person_: Person? } class PerfectFamily : Codable { var person: Person? } let jsonString = "{\"person\":\"{\\\"name\\\":\\\"Mike\\\",\\\"hobby\\\":\\\"fishing\\\"}\"}" do { // I could do this. let family = try JSONDecoder().decode(Family.self, from: Data(jsonString.utf8)) family.person_ = try

My structure does not conform to protocol 'Decodable' / 'Encodable'

不想你离开。 提交于 2020-06-28 08:07:43
问题 I was trying to use Codable to save my data from the app I am creating but when I put Codable into my structure I keep getting the error: Type 'ReminderGroups' does not conform to protocol 'Decodable' and Type 'ReminderGroups' does not conform to protocol 'Encodable' struct ReminderGroups: Codable { var contentsArray: [ReminderItem] = [] var reminderName: String = "" var reminderItem: UIImage = #imageLiteral(resourceName: "Folder") } 回答1: In order for a class or a struct to conform to a

My structure does not conform to protocol 'Decodable' / 'Encodable'

旧时模样 提交于 2020-06-28 08:07:19
问题 I was trying to use Codable to save my data from the app I am creating but when I put Codable into my structure I keep getting the error: Type 'ReminderGroups' does not conform to protocol 'Decodable' and Type 'ReminderGroups' does not conform to protocol 'Encodable' struct ReminderGroups: Codable { var contentsArray: [ReminderItem] = [] var reminderName: String = "" var reminderItem: UIImage = #imageLiteral(resourceName: "Folder") } 回答1: In order for a class or a struct to conform to a

Parse UIColor from Json file with Codable (Swift)

天涯浪子 提交于 2020-06-27 08:05:09
问题 Im trying to decode a json file, and i have alot of ui configurations there, and im looking for a clean solution to parse directly a hex code to UIColor. But UIColor doesnt conform to Codable. For example this json: var json = """ { "color": "#ffb80c" } """.data(using: .utf8)! and i want to be able to do this: struct Settings: Decodable { var color: UIColor } and while im decoding convert the "hex" string into a UIColor I already have this function to decode from a String and return a UIColor

How do I properly use Codable, CoreData and NSSet relationships?

被刻印的时光 ゝ 提交于 2020-06-09 04:47:21
问题 I'm following this tutorial to implement CoreData with Codable . Everything seems to be going great however I cannot figure out how to encode my list of photo objects. You can see my data structure in the image below and view my current code. When I try to decode the photos objects in the Pin class as below I get the error: Referencing instance method 'encode(_:forKey:)' on 'Optional' requires that 'NSSet' conform to 'Encodable' Photo+CoreDataClass.swift import Foundation import CoreData

Implementing Codable for ARAnchor: “cannot be automatically synthesized in an extension…”

喜欢而已 提交于 2020-06-08 13:17:04
问题 The code extension ARAnchor: Codable {} produces the error: "Implementation of 'Decodable' cannot be automatically synthesized in an extension in a different file to the type". What does this mean? I was able to implement Codable for another native type in a similar fashion without any errors. 回答1: You could create a container object that implements Codable and then use that to encode and decode the anchor. I tried this code in a playground and it work for me. You'll want to adapt it for

Convert received Int to Bool decoding JSON using Codable

寵の児 提交于 2020-05-26 11:57:27
问题 I have structure like this: struct JSONModelSettings { let patientID : String let therapistID : String var isEnabled : Bool enum CodingKeys: String, CodingKey { case settings // The top level "settings" key } // The keys inside of the "settings" object enum SettingsKeys: String, CodingKey { case patientID = "patient_id" case therapistID = "therapist_id" case isEnabled = "is_therapy_forced" } } extension JSONModelSettings: Decodable { init(from decoder: Decoder) throws { // Extract the top

Swift: How to add more than one item to JSON array?

僤鯓⒐⒋嵵緔 提交于 2020-05-17 06:23:27
问题 I am trying to create a basic to-do application for command-line in Swift. Below is my function to add a new item to the to-do array, but the new entry keeps overwriting the old one, instead of creating a new one. In the end, there is only one entry in the todo.json file. When I multiply the entries and .append statements manually it works, but probably my brain is too dead to figure it out at the moment. struct TodoItem: Codable { let name: String } var todoList = [TodoItem]() func addToList