codable

Enum with Raw value, Codable

回眸只為那壹抹淺笑 提交于 2019-12-20 03:52:30
问题 The following code doesn't compile: enum Occupation: String { case designer = "Designer" case engineer = "Engineer" } public struct SteveJobs: Codable { let name: String let occupation: Occupation } On the other hand, it should compile since the Occupation is represented as a String which is Codable . Why can't I use enum with raw value in Codable structs? In particular, why automatic conformance isn't working in such a case. 回答1: Automatic Codable synthesis is “opt-in,” i.e. you have to

JSONEncoder and PropertyListEncoder don't conform to Encoder?

ぐ巨炮叔叔 提交于 2019-12-20 02:06:59
问题 I am trying to write an Encoder / Decoder pair that wraps an given Encoder / Decoder . The goal of this wrapper is to successfully handle cyclic references (which neither JSONEncoder nor PropertyListEncoder do), and delegate the actual encoding/decoding to the wrappded encoder. I was surprised to learn that neither JSONEncoder nor PropertyListEncoder conform to Encoder and similarly, their Decoder counterparts don't conform to Decoder ! This seems bizarre. If these two types don't conform,

JSONEncoder and PropertyListEncoder don't conform to Encoder?

流过昼夜 提交于 2019-12-20 02:05:53
问题 I am trying to write an Encoder / Decoder pair that wraps an given Encoder / Decoder . The goal of this wrapper is to successfully handle cyclic references (which neither JSONEncoder nor PropertyListEncoder do), and delegate the actual encoding/decoding to the wrappded encoder. I was surprised to learn that neither JSONEncoder nor PropertyListEncoder conform to Encoder and similarly, their Decoder counterparts don't conform to Decoder ! This seems bizarre. If these two types don't conform,

Codable enum with default case in Swift 4

痴心易碎 提交于 2019-12-18 13:59:11
问题 I have defined an enum as follows: enum Type: String, Codable { case text = "text" case image = "image" case document = "document" case profile = "profile" case sign = "sign" case inputDate = "input_date" case inputText = "input_text" case inputNumber = "input_number" case inputOption = "input_option" case unknown } that maps a JSON string property. The automatic serialization and deserialization works fine, but I found that if a different string is encountered, the deserialization fails. Is

Swift 4 Codable: Converting JSON return String to Int/Date/Float

醉酒当歌 提交于 2019-12-18 13:23:07
问题 I'm going through some projects and removing JSON parsing frameworks, as it seems pretty simple to do with Swift 4. I've encountered this oddball JSON return where Ints and Dates are returned as Strings . I looked at GrokSwift's Parsing JSON with Swift 4, Apple's website, but I don't see anything that jumps out re: changing types. Apple's example code shows how to change key names, but I'm having a hard time figuring out how to change the key type. Here's what it looks like: { "WaitTimes": [

How to handle partially dynamic JSON with Swift Codable?

和自甴很熟 提交于 2019-12-18 11:10:02
问题 I've got some JSON messages coming in over a websocket connection. // sample message { type: "person", data: { name: "john" } } // some other message { type: "location", data: { x: 101, y: 56 } } How can I convert those messages into proper structs using Swift 4 and the Codable protocol? In Go I can do something like: "Hey at the moment I only care about the type field and I'm not interested in the rest (the data part)." It would look like this type Message struct { Type string `json:"type"`

Swift Codable expected to decode Dictionary<String, Any>but found a string/data instead

非 Y 不嫁゛ 提交于 2019-12-18 05:46:10
问题 I have been working with the Codable protocol Here is my JSON file : { "Adress":[ ], "Object":[ { "next-date":"2017-10-30T11:00:00Z", "text-sample":"Some text", "image-path":[ "photo1.png", "photo2.png" ], "email":"john.doe@test.com", "id":"27" }, { "next-date":"2017-10-30T09:00:00Z", "text-sample":"Test Test", "image-path":[ "image1.png" ], "email":"name.lastename@doe.com", "id":"28" } ] } I only have to focus on the Object array, and the "image-path" array can contain 0, 1, or 2 strings. So

Swift Decodable Optional Key

萝らか妹 提交于 2019-12-17 18:38:21
问题 (This is a follow-up from this question: Using Decodable protocol with multiples keys.) I have the following Swift code: let additionalInfo = try values.nestedContainer(keyedBy: UserInfoKeys.self, forKey: .age) age = try additionalInfo.decodeIfPresent(Int.self, forKey: .realage) I know that if I use decodeIfPresent and don't have the property it will still handle it correctly if it's an optional variable. For example the following JSON works to parse it using the code above. { "firstname":

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

落花浮王杯 提交于 2019-12-17 10:38:25
问题 I am using Codable protocol from Swift 4 first time, I am not able to understand use of decodeIfPresent from Decodable . /// Decodes a value of the given type for the given key, if present. /// /// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call. /// /// - parameter type: The type of value to decode. /// - parameter key: The key that the decoded

Swift 4 decoding json using Codable

柔情痞子 提交于 2019-12-17 09:48:30
问题 Can someone tell me what I'm doing wrong? I've looked at all the questions on here like from here How to decode a nested JSON struct with Swift Decodable protocol? and I've found one that seems exactly what I need Swift 4 Codable decoding json. { "success": true, "message": "got the locations!", "data": { "LocationList": [ { "LocID": 1, "LocName": "Downtown" }, { "LocID": 2, "LocName": "Uptown" }, { "LocID": 3, "LocName": "Midtown" } ] } } struct Location: Codable { var data: [LocationList] }