codable

Why #Codable# not working in below code?

╄→гoц情女王★ 提交于 2019-12-25 18:45:59
问题 I have below code to test Codable protocol and JSONDecoder . import UIKit class ClassA: Codable { var age: Int = 1 } class ClassB: Codable { var ageInfo: ClassA? var name: String } let json4 = """ { "ageInfo": {}, "name": "Jack" } """.data(using: .utf8)! do { let d = try JSONDecoder().decode(ClassB.self, from: json4) } catch let err { print(err) } My question is, why json4 can't be decode? or how I can decode json4 ? 回答1: age in ClassA is declared non-optional so the key is required however

Parsing the JSON data with Codable

对着背影说爱祢 提交于 2019-12-25 18:12:47
问题 I have a JSON response that I am parsing with the help of codable models. Even though my models look good, I am getting the below error, FAILURE: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “data”, intValue: nil) The JSON response is: { "status": "success", "message": "successfully.", "user": { "username": "admin", "profileImage": "/storage/default.png" }, "data": { "cash": { "withdrawableCash": "$99999910",

Decoding Nested JSON with Optional Values Swift 4

删除回忆录丶 提交于 2019-12-25 03:56:39
问题 I am trying to decode this JSON from this url https://jsonodds.com/api/test/odds Sometimes some of the values are nil and I wanted to avoid making all of my properties optional so I am trying to implement custom decoding. [ { "ID": "e83bf033-dbde-49ac-a073-d385ebdc66d1", "HomeTeam": "Xavier", "AwayTeam": "Marquette", "Sport": 2, "MatchTime": "2018-01-24T23:30:00", "Odds": [ { "ID": "50742eed-a1c8-4958-8678-50627ffb665e", "EventID": "e83bf033-dbde-49ac-a073-d385ebdc66d1", "MoneyLineHome": "0",

CoreData and Codable class compiler error: 'self.init' isn't called on all paths before returning from initializer

二次信任 提交于 2019-12-25 01:53:45
问题 After following instructions from this answer: https://stackoverflow.com/a/46917019/6047611 I am running into the compiler error 'self.init' isn't called on all paths before returning from initializer. super.init() isn't allowed. However, calling self.init(entity: entity, insertInto: context) and then initializing all of the properties in the class somehow doesn't fully initialize the class. I'm lost. I'm pretty new to CoreData and am not comfortable with it yet, so I'm hoping this is an

iOS error 'Invalid type in JSON write (FIRTimestamp)'

痴心易碎 提交于 2019-12-25 01:37:58
问题 I am trying to map my data to Model. Where I am using Firestore snapshot listener, to get data. here, I am getting data and mapping to "User" model that; do{ let user = try User(dictionary: tempUserDic) print("\(user.firstName)") } catch{ print("error occurred") } Here is my Model: struct User { let firstName: String // var lon: Double = 0.0 // var refresh:Int = 0 // var createdOn: Timestamp = Timestamp() } //Testing Codable extension User: Codable { init(dictionary: [String: Any]) throws {

NSUserDefaultsController: “Attempt to set a non-property-list object … as an NSUserDefaults/CFPreferences value for key …”

◇◆丶佛笑我妖孽 提交于 2019-12-25 01:09:01
问题 I'm trying to use NSUserDefaultsController to implement a Preferences window in my Swift macOS app. One of the settings that I need to persist is an array of presets, as defined by the following class: class Preset: NSObject { var name = "abc" var value = 123 override init() { super.init() } } Therefore, I need to persist a variable of type [Presets] . The visual representation in my Preferences window is an NSTableView , bound to an NSArrayController . I followed this tutorial to set up my

Encode struct and convert to dictionary [String : Any]

为君一笑 提交于 2019-12-24 20:19:41
问题 I'm a bit stuck on how I can post this json array using Alamofire. I've looked at How can I use Swift’s Codable to encode into a dictionary? and a few others but I can't seem to get it work. I'm appending a few rows from a UITableView it looks like this before encoding. [proj.DetailViewModel(Quantity: 1, RedeemedLocationID: 6, Points: 10), proj.DetailViewModel(Quantity: 2, RedeemedLocationID: 6, Points: 12)] struct DetailViewModel: Codable { var Quantity: Int! var RedeemedLocationID: Int! var

Saving model into Userdefaults crashes application swift [duplicate]

China☆狼群 提交于 2019-12-24 19:25:15
问题 This question already has answers here : How to save list of object in user Default? (6 answers) Closed 6 months ago . I am trying to save my model class in UserDefault but keep getting error of -[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x2825fc0a0' I am using codables and below is what my Model looks like struct AuthModel: Codable { let token: String? let firstName: String? let lastName: String? let telephone: String? let userId: Int? let email: String? let

Codable and NSManaged subclass with separate saving and decoding operations

冷暖自知 提交于 2019-12-24 17:12:59
问题 My requirement is that I will get some JSON data from the server and I need to decode it and sometimes save it to CoreData. This is to ensure that I don't need to keep 2 separate Models each for Decoding and CoreData but one that will handle both. I went through a few threads related to Codable CoreData but what they offer is that everytime we are performing Decoding , the object gets saved to the CoreData(which first of all is not what I want and second it creates duplicate entries) like

Refining Swift API GET Functions

强颜欢笑 提交于 2019-12-24 10:14:15
问题 I'm working on a practice project where the iOS app prints a list of /posts from jsonplaceholder.typicode.com, and when the user selects one a detailed view controller is loaded and further information about that post is displayed (author and number of comments). I've made three separate GET requests for the three different endpoints, as each of them require different return types and different parameters (or none at all). I wanted to take as much code as possible that's in common between the