Closed murad1981 closed 9 months ago
There are two ways you can go about this:
For your case, 2nd point seems more appropriate as it will handle validation when you want to initialize with member-wise initializer.
Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.
Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.
You can follow these articles on how to provide custom Codable
implementations on custom types:
https://medium.com/@pxpgraphics/decoding-custom-types-with-decodable-in-swift-4-c730597da373 https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/
Also additionally you can have a look at primitive obsession and how to avoid it:
https://medium.com/@amlcurran/avoiding-primitive-obsession-in-swift-5325b65d521e
Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.
You can follow these articles on how to provide custom
Codable
implementations on custom types:https://medium.com/@pxpgraphics/decoding-custom-types-with-decodable-in-swift-4-c730597da373 https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/
Also additionally you can have a look at primitive obsession and how to avoid it:
https://medium.com/@amlcurran/avoiding-primitive-obsession-in-swift-5325b65d521e
Thanks for the reply, but I know all of those stuff, if I want to create a custom type for a specific property to handle encoding/decoding that means i will not use MetaCodable
. I've built a custom HelperCoder which has some boilerplate code that I didn't like.
Thanks for the reply, but I know all of those stuff, if I want to create a custom type for a specific property to handle encoding/decoding that means i will not use
MetaCodable
. I've built a custom HelperCoder which has some boilerplate code that I didn't like.
While you can go ahead with using MetaCodable
to generate HelperCoder
, what you are looking for in this particular case is not of scope for MetaCodable
.
If you go by the definition of type:
a type is a description of a set of values and a set of allowed operations on those values.
From your requirements, it seems you are trying to impose restrictions on existing type (which is a classic example of primitive obsession) rather than creating new type with said restrictions.
The purpose of HelperCoder
is to have transformations of representation of various forms of data (i.e. converting date to string or vice versa).
MetaCodable
is designed to generate Codable
implementation for custom types rather than impose restriction on operation performed with said types.
I am closing this issue since you have gone ahead with your solution and I see nothing in scope of MetaCodable
.
I have the following model:
is there a way to convert
myVar
's value to nil when its value is empty string when encoding/ decoding the model ?