-
How does SwiftUI know the default value for Decoder when we never provided a default value for it? Original question: https://twitter.com/dev_seungjun/status/1512509304238387202 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
init vs init(from:)Before defining let ddFromDecoder = User2(from: /* some decoder */) With the addition of let dd = User2()
let ddFromDecoder = User2(from: /* some decoder */) Note that the reason "we don't need to pass parameter for 'from'" is because class User2: Codable ... {
required init(from decoder: Decoder) { print("from decoder"); ... }
init() { print("default") }
}
let dd = User2() // prints "default" only Who used init(from:)?You've never called // from day 49, decodedResponse is an instance of type Response
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
results = decodedResponse.results
}
// from day 51, decodedOrder is an instance of type Order
let decodedOrder = try JSONDecoder().decode(Order.self, from: data)
confirmationMessage = "Your order for \(decodedOrder.quantity)x \(Order.types[decodedOrder.type].lowercased()) cupcakes is on its way!"
... Looks like How does JSONDecoder work?Luckily, Swift open-sourced its Foundation library for us to see how open func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
do {
var parser = JSONParser(bytes: Array(data))
let json = try parser.parse()
return try JSONDecoderImpl(userInfo: self.userInfo, from: json, codingPath: [], options: self.options).unwrap(as: T.self)
} catch ...
} The return statement initialized a func unwrap<T: Decodable>(as type: T.Type) throws -> T {
...
return try T(from: self)
} ..., where it calls the extension JSONDecoderImpl: Decoder { Now we have the answer: when we call Is there a default value?
let decodedUserFromPLIST = try PropertyListDecoder().decode(User2.self, from: /* data from plist file */) ... to get an instance of Therefore, there is no default value for the Decoder. It depends on what kind of decoder you use, allowing your Codable types to work with many kinds of decoders/formats. |
Beta Was this translation helpful? Give feedback.
init vs init(from:)
Before defining
init()
, the only way to construct an instance ofUser2
is using theinit(from decoder: Decoder)
(I'll refer to it asinit(from:)
for short), and you must pass an argument that conforms to the Decoder protocol when trying to construct a User2, like:With the addition of
init()
, there are now 2 ways to construct an instance ofUser2
, by either callinginit()
orinit(from:)
with an argument:Note that the reason "we don't need to pass parameter for 'from'" is because
User2()
doesn't callinit(from:)
; it calls the newinit()
that you jus…