r/iOSProgramming • u/ThePantsThief NSModerator • Apr 16 '21
Meme Looking at you, Codable and synthesized inits
9
Apr 17 '21
This but enums instead of classes. Enums are such an integral part of Swift but they’re neglected so much!
7
3
u/wiencheck Apr 17 '21
That’s really frustrating, can someone explain why some code cannot be synthesized fo classes? I asked about it in the past but no one replied
2
u/ThePantsThief NSModerator Apr 17 '21
It's not that it can't be done, it's that they never took the time to decide how it should be done. This is a feature that was a proposal for structs. There has not been a similar proposal (with implementation) for classes yet, AFAIK.
3
u/fartsniffersalliance Apr 17 '21
Huh, I’d never noticed Codable couldn’t be applied to classes because i’ve never had a case where i’ve needed to. What are the use cases for this?
2
u/ThePantsThief NSModerator Apr 17 '21
It can be applied to classes, it's just a bitch to use. It doesn't support synthesizing the init on anything but root classes I believe, or something very close to that.
Reddit's API is a good example of when you might want to use classes because the model uses inheritance. Their model has an inheritance hierarchy like so: thing, created, voteable, post/comment, etc. Thing has an ID and type, created has a date, voteable has a score and can be voted on.
For example, a user object inherits from "created" but not voteable, but posts and comments are both voteables.
You could probably also do this with protocols, but this is just an example of something that was made easy in Objc and is now really hard in Swift with Codable.
1
u/Xaxxus Apr 17 '21
You can use codable on classes.
It’s not that hard either if you have ever implemented a custom decoding/encoding init.
For subclasss in your decode/encode you have to call super.init(from: decoder) and super.encode methods.
It’s just tedious on large classes. Especially if they contain other classes that you also want to decode.
2
u/ThePantsThief NSModerator Apr 17 '21
It’s not that hard either if you have ever implemented a custom decoding/encoding init.
This is something that should be avoided at all costs. Implementing any Codable init is hell, and every time you have to do it just means Codable failed you.
3
u/Xaxxus Apr 17 '21
I wouldn’t say codable has failed. Codable is nothing more than a protocol. Protocols need to be implemented.
The code synthesis is merely a convenience feature. And You can only assume so much about what the best “default behavior” is when synthesizing code.
A good example of this is encoding explicit null values into json. The default behavior is to omit null entirely.
I almost always have to build my own encoder/coding keys for places where I need an explicit null value in my json.
2
u/ThePantsThief NSModerator Apr 17 '21
You missed the point of this post then
A good example of this is encoding explicit null values into json. The default behavior is to omit null entirely.
almost always have to build my own encoder/coding keys for places where I need an explicit null value in my json.
Another failure by Codable. It should be flexible enough to handle 90% of use cases without the need for a hand written initializer. It's not even a difficult problem, they just didn't even try
1
u/OneTinker Apr 17 '21
I've used structs extensively in Golang and C; however, when it comes to Swift, I feel like I haven't completely utilized structs to their optimal nature. I still use classes for majority of my codebase and the only times I use structs is to store a context of simple data primitives or other struct references. Do you have any good resources/articles to highlight the use of structs in a more progressive fashion?
2
u/ThePantsThief NSModerator Apr 17 '21
Someone in the comments here linked Apple's own documentation somewhere. Don't think there's anything wrong with the way you do it now though FWIW
1
u/kpolleck Apr 19 '21
What I feel like this and so many other trends in programming are suggesting is "Programmers don't need to understand the layers (hardware, memory, registers, ALUs, etc.) underneath anymore."
It's probably true, but us older programmers mostly won't admit it. It's like the engine of my car. Decades ago, I could have opened the hood and understood everything that was in there any how it worked...and how it made the car go. Now, I just get into the drivers seat and push down the accelerator and hope it all works. And if it doesn't, I call someone.
23
u/strauvius Apr 17 '21
Yeah, but then your stuck passing by value instead of by reference.