You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then implement Codable for all shapes. How do you encode/decode Canvas with these class types?
I can't encode class type into Shape itself because by the time Shape's required init(from decoder: Decoder) throws is called, it is already in Shape's initializer, so I can't initialize Rectangle nor Oval.
So, I ended up having wrapper struct that hold class type as string, and class reference to encode something like this:
Attachment: Download
Environment
Xcode 9 beta 2
Additional Detail from JIRA
md5: 037f67612a560e973f40d26c4e04b646
duplicates:
Issue Description:
I'm trying to migrate my app using NSCoder into Swift4's Codable. However, I'm struggling using Codable for classes.
Say, you have
Shape
class and 2 subclassesRectangle
andOval
. AndCanvas
contains multiple shapes as {{ var shapes: [Shape] }}.Then implement Codable for all shapes. How do you encode/decode Canvas with these class types?
I can't encode class type into
Shape
itself because by the timeShape
'srequired init(from decoder: Decoder) throws
is called, it is already inShape
's initializer, so I can't initializeRectangle
norOval
.So, I ended up having wrapper struct that hold class type as string, and class reference to encode something like this:
(Playground file is attached.)
This would work, but I'm wondering... is there any better way to encode/decode with array base class type of array?
The text was updated successfully, but these errors were encountered: