codable

Create codable struct with generic type

北城余情 提交于 2019-11-28 14:20:23
First of all, Sorry for unclear title of question I'm making a codable struct which will be used as json message. enum MessageType: String, Codable{ case content case request case response } struct Message: Codable{ var type: MessageType var content: /* NEED HELP HERE */ } struct Content: Codable {...} struct Request: Codable {...} struct Response: Codable {...} When declaring Message , if its type is content , its content 's type should be Content . let message = Message( type: .content, content: Content( ... ) } When type is request , its content 's type should be Request . let message =

Swift 4 Codable - Bool or String values

六眼飞鱼酱① 提交于 2019-11-28 13:07:41
Looking for some input as to how you would handle the scenario I recently ran into. I have been using Swift 4s Codable with success but today noticed a crash that I didn't expect. The API that I am working with, says it returns a boolean for the key manage_stock . My stubbed struct looks like: struct Product: Codable { var manage_stock: Bool? } That works fine, the problem is that the API sometimes returns a string instead of a boolean . Because of this, my decode fails with: Expected to decode Bool but found a string/data instead. The string only ever equals "parent" and I want it to equate

Do all attributes with a custom type in core-data have to be a relationship?

北慕城南 提交于 2019-11-28 12:19:40
问题 This is a followup question from the comments in the following How to map nested complex JSON objects and save them to core data?. Imagine that I already have this code for my app. class Passenger{ var name: String var number: String var image : UIImage // init method } class Trip { let tripNumber : Int let passenger : Passenger init(tripNumber: Int, passenger: Passenger) { self.tripNumber = tripNumber self.passenger = passenger } } Now I've decided to add persistence for my app. I just want

Swift's JSONDecoder with multiple date formats in a JSON string?

懵懂的女人 提交于 2019-11-28 09:01:16
Swift's JSONDecoder offers a dateDecodingStrategy property, which allows us to define how to interpret incoming date strings in accordance with a DateFormatter object. However, I am currently working with an API that returns both date strings ( yyyy-MM-dd ) and datetime strings ( yyyy-MM-dd HH:mm:ss ), depending on the property. Is there a way to have the JSONDecoder handle this, since the provided DateFormatter object can only deal with a single dateFormat at a time? One ham-handed solution is to rewrite the accompanying Decodable models to just accept strings as their properties and to

What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

家住魔仙堡 提交于 2019-11-28 08:57:35
问题 I am getting the following JSON back from a network request: { "uvIndex": 5 } I use the following type to decode the string-to-data result: internal final class DataDecoder<T: Decodable> { internal final class func decode(_ data: Data) -> T? { return try? JSONDecoder().decode(T.self, from: data) } } The following is the model that the data is to be transformed into: internal struct CurrentWeatherReport: Codable { // MARK: Properties internal var uvIndex: Int? // MARK: CodingKeys private enum

Swift 4 JSON Codable ids as keys

走远了吗. 提交于 2019-11-28 06:38:06
问题 I would like to use Swift 4's codable feature with json but some of the keys do not have a set name. Rather there is an array and they are ids like { "status": "ok", "messages": { "generalMessages": [], "recordMessages": [] }, "foundRows": 2515989, "data": { "181": { "animalID": "181", "animalName": "Sophie", "animalBreed": "Domestic Short Hair / Domestic Short Hair / Mixed (short coat)", "animalGeneralAge": "Adult", "animalSex": "Female", "animalPrimaryBreed": "Domestic Short Hair",

How to use Codable protocol in objective c data model class?

淺唱寂寞╮ 提交于 2019-11-28 03:32:36
问题 I am working on mix and match iOS source code. I have implemented codable for swift data model class which reduces the burden of writing parser logic. I tried to conform my objective c class to codable protcol which in turn thrown an error "Cannot find protocol declaration for 'Codable'". Is there any way to use this swift protocol into objective c class? Or Is there any other objective c api that provides the same capability as Codable? The idea is to make the parsing logic same across swift

Swift String escaping when serializing to JSON using Codable

冷暖自知 提交于 2019-11-28 02:26:58
问题 I'm trying to serialize my object as following: import Foundation struct User: Codable { let username: String let profileURL: String } let user = User(username: "John", profileURL: "http://google.com") let json = try? JSONEncoder().encode(user) if let data = json, let str = String(data: data, encoding: .utf8) { print(str) } However on macOS I'm getting the following: {"profileURL":"http:\/\/google.com","username":"John"} (note escaped '/' character). While on Linux machines I'm getting: {

Flattening JSON when keys are known only at runtime

一笑奈何 提交于 2019-11-28 02:21:25
问题 Let's say we have a JSON structure like the following (commonly used in Firebase's Realtime Database): { "18348b9b-9a49-4e04-ac35-37e38a8db1e2": { "isActive": false, "age": 29, "company": "BALOOBA" }, "20aca96e-663a-493c-8e9b-cb7b8272f817": { "isActive": false, "age": 39, "company": "QUONATA" }, "bd0c389b-2736-481a-9cf0-170600d36b6d": { "isActive": false, "age": 35, "company": "EARTHMARK" } } Expected solution: Using Decodable I'd like to convert it into an array of 3 elements: struct

Using Codable on a dynamic type/object

非 Y 不嫁゛ 提交于 2019-11-27 20:54:05
Hi I have the following structure nested in a bigger structure that is returned from an api call but I can't manage to encode/decode this part. The problem I am having is that the customKey and customValue are both dynamic. { "current" : "a value" "hash" : "some value" "values": { "customkey": "customValue", "customKey": "customValue" } } I tried something like var values: [String:String] But that is obviously not working because its not actually an array of [String:String] . Since you linked to my answer to another question, I will expand that one to answer yours. Truth is, all keys are known