encodable

Fastest way to save structs iOS / Swift

孤街浪徒 提交于 2021-01-20 07:13:36
问题 I have structs like struct RGBA: Codable { var r: UInt8 var g: UInt8 var b: UInt8 var a: UInt8 } I want save large amount of this structs (>1_000_000) Decode guard let history = try? JSONDecoder().decode(HistoryRGBA.self, from: data) else { return } Encode guard let jsonData = try? encoder.encode(dataForSave) else { return false } How can I improve encoding/decoding time and amount of RAM memory? 回答1: Considering that all your properties are UInt8 (bytes) you can make your struct conform to

Fastest way to save structs iOS / Swift

狂风中的少年 提交于 2021-01-20 07:13:09
问题 I have structs like struct RGBA: Codable { var r: UInt8 var g: UInt8 var b: UInt8 var a: UInt8 } I want save large amount of this structs (>1_000_000) Decode guard let history = try? JSONDecoder().decode(HistoryRGBA.self, from: data) else { return } Encode guard let jsonData = try? encoder.encode(dataForSave) else { return false } How can I improve encoding/decoding time and amount of RAM memory? 回答1: Considering that all your properties are UInt8 (bytes) you can make your struct conform to

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

How to Decode selected keys manually and rest with the automatic decoding with swift Decodable?

ε祈祈猫儿з 提交于 2020-01-24 20:14:48
问题 Here is the code I am using, struct CreatePostResponseModel : Codable{ var transcodeId:String? var id:String = "" enum TopLevelCodingKeys: String, CodingKey { case _transcode = "_transcode" case _transcoder = "_transcoder" } enum CodingKeys:String, CodingKey{ case id = "_id" } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: TopLevelCodingKeys.self) if let transcodeId = try container.decodeIfPresent(String.self, forKey: ._transcode) { self.transcodeId =

JSONEncoder's dateEncodingStrategy not working

随声附和 提交于 2019-12-20 02:59:08
问题 I am trying to serialize a struct to a String using Swift 4's Encodable+JSONEncoder. The object can hold heterogenous values like String, Array, Date, Int etc. The used approach works fine with the exception of Date. JSONEncoder's dateEncodingStrategy property is not having any effect. Here is a snippet which reproduces the behaviour in Playground: struct EncodableValue:Encodable { var value: Encodable init(_ value: Encodable) { self.value = value } func encode(to encoder: Encoder) throws {