Open swift-ci opened 5 years ago
This is correct behavior. It may not be correct to just produce nil
here—what if it's "list of users forbidden from accessing my secret data"? Providing custom decoding behavior via init(coder:)
is the right way to communicate that to the compiler.
I'm leaving this open because it's a reasonable feature request for some kind of annotation on the property to treat unknown values as nil.
Comment by Ta Phuc Thanh (JIRA)
I got it. Thanks @belkadan for your answer!
Attachment: Download
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, Codable | |Assignee | None | |Priority | Medium | md5: 9892ca2b5bd246ad11addcb4eddc868eIssue Description:
Swift Codable is an awesome feature that i'm using very regularly. But while working with it I found an issue.
So I have a basic struct like this:
And i tried to parse `Parent` with an undefined value from `Children`, i.e:
The issue is instead of receving a Parent instance with
childrean = nil
, it through me a serialization error:Please refer to the playground file i attached below. Note that it will work if i define my own decode init function. (can just uncomment
public init(from decoder: Decoder) throws