You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
swift-ci opened this issue
Mar 1, 2018
· 4 comments
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.CodableArea → standard library: `Codable` and co.compilerThe Swift compiler in itself
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
Working on an iOS application with the Swift compiler language set to 3.2.
Additional Detail from JIRA
Votes
0
Component/s
Compiler
Labels
Bug, Codable
Assignee
None
Priority
Medium
md5: 46c3a50f989d441361b08d992ddc0f99
Issue Description:
Decoding JSON with JSONDecoder into a Codable conforming class with properties with default values doesn't work as expected. If the JSON has those properties set with a value, this value gets ignored and the default value of the property gets set.
For example, lets say I have this class:
public class Example: Codable {
{{ public let name: String = ""}}
{{}}}
And this JSON inside a Data property called data:
{name: "Some Name"}
When doing the following, the 'name' property has a value of "" and not "Some Name".
let example = try? JSONDecoder().decode(Example.self, from: data)
If I remove the default value and move it into an initializer, decoding works as expected and the 'name' property ends up with a value of "Some Name":
public class Example: Codable {
public let name: String
init() { name = "" }
}
The text was updated successfully, but these errors were encountered:
I'm sorry, I got the names wrong. Anyways, I get what you mean, your example is clearly wrong. The point is that the idea of having an initial value in the property declaration site or in the initializer is the same, but decoding only works properly when using initial values in the initializer and not in the property declaration site.
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.CodableArea → standard library: `Codable` and co.compilerThe Swift compiler in itself
Environment
macOS High Sierra 10.3.3.
Xcode Version 9.2 (9C40b)
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
Working on an iOS application with the Swift compiler language set to 3.2.
Additional Detail from JIRA
md5: 46c3a50f989d441361b08d992ddc0f99
Issue Description:
Decoding JSON with JSONDecoder into a Codable conforming class with properties with default values doesn't work as expected. If the JSON has those properties set with a value, this value gets ignored and the default value of the property gets set.
For example, lets say I have this class:
public class Example: Codable {
{{ public let name: String = ""}}
{{}}}
And this JSON inside a Data property called data:
{name: "Some Name"}
When doing the following, the 'name' property has a value of "" and not "Some Name".
let example = try? JSONDecoder().decode(Example.self, from: data)
If I remove the default value and move it into an initializer, decoding works as expected and the 'name' property ends up with a value of "Some Name":
public class Example: Codable {
public let name: String
init() { name = "" }
}
The text was updated successfully, but these errors were encountered: