codable

Swift Codable null handling

浪尽此生 提交于 2019-12-03 14:41:57
I have a struct that parse JSON using Codable . struct Student: Codable { let name: String? let amount: Double? let adress: String? } Now if the amount value is coming as null the JSON parsing is failing. So should I manually handle the null cases for all the Int and Double that are present in the Student struct? The String values coming as null is automatically handled. Let me do this Playground for you since an example shows you more than a hundred words: import Cocoa struct Student: Codable { let name: String? let amount: Double? let adress: String? } let okData = """ { "name": "here",

How to use computed property in a codable struct (swift)

安稳与你 提交于 2019-12-03 12:19:12
I've created a "codable" struct to serialize a data set and encode it to Json. Everything is working great except the computed properties don't show in the json string. How can I include computed properties during the encode phase. Ex: struct SolidObject:Codable{ var height:Double = 0 var width:Double = 0 var length:Double = 0 var volume:Double { get{ return height * width * length } } } var solidObject = SolidObject() solidObject.height = 10.2 solidObject.width = 7.3 solidObject.length = 5.0 let jsonEncoder = JSONEncoder() do { let jsonData = try jsonEncoder.encode(solidObject) let jsonString

Swift Codable protocol with recursive enums

本小妞迷上赌 提交于 2019-12-03 08:23:41
Let's say that I have a model like the following, which allows me to build a tree of Foo objects. struct Foo { var kind : Kind enum Kind { case node([Foo]) case leaf } } How can I make this Codable, specifically for the case node([Foo]) ? Here's the final struct, based on the answer from @PauloMattos: Base Foo struct: struct Foo { var name: String var kind: Kind enum Kind { case node([Foo]) case leaf } init(name: String, kind: Kind) { self.name = name self.kind = kind } } Codable Protocol extension: extension Foo : Codable { enum CodingKeys: String, CodingKey { case name case nodes } enum

How can I implement polymorphic decoding of JSON data in Swift 4?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 06:39:35
I am attempting to render a view from data returned from an API endpoint. My JSON looks (roughly) like this: { "sections": [ { "title": "Featured", "section_layout_type": "featured_panels", "section_items": [ { "item_type": "foo", "id": 3, "title": "Bisbee1", "audio_url": "http://example.com/foo1.mp3", "feature_image_url" : "http://example.com/feature1.jpg" }, { "item_type": "bar", "id": 4, "title": "Mortar8", "video_url": "http://example.com/video.mp4", "director" : "John Smith", "feature_image_url" : "http://example.com/feature2.jpg" } ] } ] } I have an object that represents how to layout a

Codable class does not conform to protocol Decodable

不羁岁月 提交于 2019-12-03 06:30:08
问题 Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { case indexPath case locationInText } init(publication: Publication?, indexPath: [Int]) { self.publication = publication self.indexPath = indexPath } } I do not wish to save the publication var since the Publication owns the Bookmark but the bookmark needs to

How to exclude properties from Swift 4's Codable

风流意气都作罢 提交于 2019-12-03 04:00:55
问题 Swfit 4's new Encodable / Decodable protocols make JSON (de)serialization quite pleasant. However, I have not yet found a way to have fine-grained control over which properties should be encoded and which should get decoded. I have noticed that excluding the property from the accompanying CodingKeys enum excludes the property from the process altogether, but is there a way to have more fine-grained control? 回答1: The list of keys to encode/decode is controlled by a type called CodingKeys (note

Implementing a custom Decoder in Swift 4

99封情书 提交于 2019-12-03 03:06:23
问题 I'd like to decode an XML document using the new Decodable protocol introduced in Swift 4, however, there doesn't seem to be an existing implementation for an XML decoder that conforms to the Decoder protocol. My plan was to use the SWXMLHash library to parse the XML, then possibly make the XMLIndexer class in that library extend the Decoder protocol so that my model can be initialized with an instance of XMLIndexer ( XMLIndexer is returned by SWXMLHash.parse(xmlString) ). My issue is that I

Codable class does not conform to protocol Decodable

六月ゝ 毕业季﹏ 提交于 2019-12-02 21:43:48
Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { case indexPath case locationInText } init(publication: Publication?, indexPath: [Int]) { self.publication = publication self.indexPath = indexPath } } I do not wish to save the publication var since the Publication owns the Bookmark but the bookmark needs to know which Publication it belongs to. The decode init of Publication will set the bookmark reference to

Implementing a custom Decoder in Swift 4

佐手、 提交于 2019-12-02 17:40:51
I'd like to decode an XML document using the new Decodable protocol introduced in Swift 4, however, there doesn't seem to be an existing implementation for an XML decoder that conforms to the Decoder protocol. My plan was to use the SWXMLHash library to parse the XML, then possibly make the XMLIndexer class in that library extend the Decoder protocol so that my model can be initialized with an instance of XMLIndexer ( XMLIndexer is returned by SWXMLHash.parse(xmlString) ). My issue is that I have no clue how to implement the Decoder protocol and I can't seem to find any resources online that

How to exclude properties from Swift 4's Codable

♀尐吖头ヾ 提交于 2019-12-02 17:23:05
Swfit 4's new Encodable / Decodable protocols make JSON (de)serialization quite pleasant. However, I have not yet found a way to have fine-grained control over which properties should be encoded and which should get decoded. I have noticed that excluding the property from the accompanying CodingKeys enum excludes the property from the process altogether, but is there a way to have more fine-grained control? The list of keys to encode/decode is controlled by a type called CodingKeys (note the s at the end). The compiler can synthesize this for you but can always override that. Let's say you