Skip to content

Where is the decoder in init(from decoder: Decoder) from? #28

Answered by ApolloZhu
ApolloZhu asked this question in Q&A
Discussion options

You must be logged in to vote

init vs init(from:)

Before defining init(), the only way to construct an instance of User2 is using the init(from decoder: Decoder) (I'll refer to it as init(from:) for short), and you must pass an argument that conforms to the Decoder protocol when trying to construct a User2, like:

let ddFromDecoder = User2(from: /* some decoder */)

With the addition of init(), there are now 2 ways to construct an instance of User2, by either calling init() or init(from:) with an argument:

let dd = User2() 
let ddFromDecoder = User2(from: /* some decoder */)

Note that the reason "we don't need to pass parameter for 'from'" is because User2() doesn't call init(from:); it calls the new init() that you jus…

Replies: 1 comment 1 reply

Comment options

ApolloZhu
Apr 9, 2022
Maintainer Author

You must be logged in to vote
1 reply
@seungjun-green
Comment options

Answer selected by ApolloZhu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants