decodable

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":

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

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

Swift: Codable - extract a single coding key

帅比萌擦擦* 提交于 2020-05-10 14:20:33
问题 I have the following code to extract a JSON contained within a coding key: let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"]) This successfully handles the following JSONs: { "applmusic":{ "code":"AAPL", "quality":"good", "line":"She told me don't worry", } However, fails to extract a JSON with the coding key of applmusic from the following one: { "applmusic":{ "code":"AAPL", "quality":"good", "line":"She told me don't worry", }, "spotify":{ "differentcode":"SPOT"