codable

Swift Codable decode empty json as nil or empty object

二次信任 提交于 2020-08-23 03:05:07
问题 Here's my code: class LoginUserResponse : Codable { var result: String = "" var data: LoginUserResponseData? var mess: [String] = [] } public class LoginUserResponseData : Codable { var userId = "0" var name = "" } Now, calling the server API I'm parsing response like this (using Stuff library to simplify parsing): do { let loginUserResponse = try LoginUserResponse(json: string) } catch let error { print(error) } When I enter the correct password I'm getting an answer like this: {"result":

Swift Codable decode empty json as nil or empty object

≯℡__Kan透↙ 提交于 2020-08-23 03:04:29
问题 Here's my code: class LoginUserResponse : Codable { var result: String = "" var data: LoginUserResponseData? var mess: [String] = [] } public class LoginUserResponseData : Codable { var userId = "0" var name = "" } Now, calling the server API I'm parsing response like this (using Stuff library to simplify parsing): do { let loginUserResponse = try LoginUserResponse(json: string) } catch let error { print(error) } When I enter the correct password I'm getting an answer like this: {"result":

How to decode an array of values whose types depend on a tag?

余生长醉 提交于 2020-08-22 05:17:08
问题 I have a JSON with an array of values: [ { "tag": "Foo", … }, { "tag": "Bar", … }, { "tag": "Baz", … }, ] I want to decode this array into an array of struct s where the particular type depends on the tag: protocol SomeCommonType {} struct Foo: Decodable, SomeCommonType { … } struct Bar: Decodable, SomeCommonType { … } struct Baz: Decodable, SomeCommonType { … } let values = try JSONDecoder().decode([SomeCommonType].self, from: …) How do I do that? At the moment I have this slightly ugly

Custom Struct: Type does not conform to protocol 'Decodable'

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-10 23:16:35
问题 I would like to be able to save a Custom-struct to UserDefaults but for that I need it to be Codable .. I tried it like this: struct Wishlist: Codable { var name: String var image: UIImage var wishData: [Wish] var color: UIColor var textColor: UIColor var index: Int } But that gives me this error : Type 'Wishlist' does not conform to protocol 'Decodable' Here is my Class Wish , maybe that's where the problem is: class Wish: NSObject { public var wishName : String? public var checkedStatus :

How to conform an ObservableObject to the Codable protocols?

好久不见. 提交于 2020-08-05 02:32:46
问题 In SwiftUI beta 5, Apple introduced the @Published annotation. This annotation is currently blocking this class from conforming to the Codable protocols. How can I conform to these protocols so I can encode and decode this class to JSON? You can ignore the image property for now. class Meal: ObservableObject, Identifiable, Codable { enum CodingKeys: String, CodingKey { case id case name case ingredients case numberOfPeople } var id = Globals.generateRandomId() @Published var name: String = ""

How to make Swift Codable types more versatile

。_饼干妹妹 提交于 2020-07-19 04:28:08
问题 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" }, {