codable

Saving a Codable Struct to UserDefaults with Swift

冷暖自知 提交于 2021-02-19 04:43:08
问题 I am trying to encode a struct struct Configuration : Encodable, Decodable { private enum CodingKeys : String, CodingKey { case title = "title" case contents = "contents" } var title : String? var contents: [[Int]]? } into JSON to store in a local key of UserDefaults.standard. I have the following code: let jsonString = Configuration(title: nameField.text, contents: newContents) let info = ["row" as String: jsonString as Configuration] print("jsonString = \(jsonString)") //trying to save

Converting Firebase Datasnapshot to codable JSON data

ぃ、小莉子 提交于 2021-02-19 02:26:21
问题 I am using Firebase Database and am attempting to retrieve and use data using NSObject. I am receiving a NSUnknownKeyException error when running the app causing it to crash. NSObject: class WatchList: NSObject { var filmid: Int? } Firebase Code: ref.child("users").child(uid!).child("watchlist").observe(DataEventType.childAdded, with: { (info) in print(info) if let dict = info.value as? [String: AnyObject] { let list = WatchList() list.setValuesForKeys(dict) print(list) } }, withCancel: nil)

Trying to write a generic function for parsing JSON into codable Structs

泪湿孤枕 提交于 2021-02-17 04:45:43
问题 I'm currently parsing JSON like this struct ExampleStruct : Codable { init() { } // implementation } if let jsonData = jsonString.data(using: .utf8) { do { let decoder = JSONDecoder() let object = try decoder.decode(ExampleStruct.self, from: jsonData) } catch { print("Coding error - \(error)") } } This works fine, however I've been trying to learn about Generics over the weekend. I'm trying to write a method which I pass in a Codable struct type and a string of JSON which returns the object

Trying to write a generic function for parsing JSON into codable Structs

徘徊边缘 提交于 2021-02-17 04:45:22
问题 I'm currently parsing JSON like this struct ExampleStruct : Codable { init() { } // implementation } if let jsonData = jsonString.data(using: .utf8) { do { let decoder = JSONDecoder() let object = try decoder.decode(ExampleStruct.self, from: jsonData) } catch { print("Coding error - \(error)") } } This works fine, however I've been trying to learn about Generics over the weekend. I'm trying to write a method which I pass in a Codable struct type and a string of JSON which returns the object

Using “Codable” to set property values doesn't work through inheritance

試著忘記壹切 提交于 2021-02-16 17:55:10
问题 I am unable to set the b property in a child class. It is the parent class that inherits from Codable , and that seems to be working well. I feel like I am missing something really obvious, but I am having trouble seeing the wood for the trees. Below is a playground example of my problem. b remains 0, despite being set to 10 . It is the child class that gets passed in, but the parent property that can be set (very weird!). class Primary : Codable { var a: Int = 0 } class Secondary : Primary {

Flattened objects in JSON file to nested object structure in Swift [duplicate]

怎甘沉沦 提交于 2021-02-10 15:10:05
问题 This question already has answers here : How to decode a nested JSON struct with Swift Decodable protocol? (6 answers) Closed 8 months ago . Given a JSON object with nested object structure that looks like this: { "users":[ { "user":{ "name":"Adam", "age":25 }, "address":{ "city":"Stockholm", "country":"Sweden" } }, { "user":{ "name":"Lilly", "age":24 }, "address":{ "city":"Copenhagen", "country":"Denmark" } } ] } How can one implement correct Decodable implementation for an object that looks

NSKeyedUnarchiver decodeObjectForKey: cannot decode object of class for key (NS.objects)

[亡魂溺海] 提交于 2021-02-10 07:06:57
问题 I looked through whole SO but still no answer. My app reports this problem: Fatal Exception: NSInvalidUnarchiveOperationException *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (App_Title.Products) for key (NS.objects); the class may be defined in source code or a library that is not linked I did already do NSKeyedUnarchiver.setClass just before #unarchiveObject: func loadProducts() -> [Products]? { NSKeyedUnarchiver.setClass(Products.self, forClassName: "Products

Decoding different type with and without array

邮差的信 提交于 2021-02-10 05:08:41
问题 I am trying to decode the error as follows, most of the error that I am handling in array format [String] , but in few cases the error is not in array format, just a String . If error comes in array format name comes as errors , but if it is string format then it comes as error . How could I handle this scenario? How could I able to handle this scenario? struct CustomError: Codable { let errors: [String] } private func errorDecoding(data : Data) { let decoder = JSONDecoder() do { let

The given data did not contain a top-level value error when attempting to decode json into NSManagedObject

牧云@^-^@ 提交于 2021-02-07 10:53:49
问题 While migrating an app from Realm to CoreData, I'm attempting to parse the following JSON structure: { "username": "972542052677", "displayName": "Arik", "countryCode": "972", "status": "Busy", "walletPublicKey": "XgWDiGMFRPDRVnRq8nxu7NyMLuT4Uez7mJ", "lastSeen": "2020-08-05T08:12:57.5267881", "isOnline": false } I need to create an NSManagedObject from this data so I'm following this tutorial: https://medium.com/@andrea.prearo/working-with-codable-and-core-data-83983e77198e This is the model.

Parsing a Stock API that contain dates as keys using the Codable protocol

半腔热情 提交于 2021-02-04 06:25:46
问题 I am trying to parse a stock api from Alpha Vantage . This is what a response looks like: API Response Demo I set up Four classes to use for decoding and encoding: Stocks Meta Data Time Series Open High Low Close I think the problem lies in the Time Series Classes since i am suppose to be getting an array dates as a Key, which each contain Open, Close, High, Low values. All three classes conform to the Codable protocol.I changed the value of the keys in the enum so that it could match the