Last active
September 4, 2021 07:48
save/restore abstract objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword | |
and make this Contents class to save and load shape objects. | |
*/ | |
import Foundation | |
import CoreGraphics | |
protocol Shape { | |
} | |
class Circle: Shape { | |
var center: CGPoint | |
var radius: CGFloat | |
init(center: CGPoint, radius: CGFloat) { | |
self.center = center | |
self.radius = radius | |
} | |
} | |
class Rectangle: Shape { | |
var origin: CGPoint | |
var size: CGSize | |
init(origin: CGPoint, size: CGSize) { | |
self.origin = origin | |
self.size = size | |
} | |
} | |
class RoundedRectangle: Rectangle { | |
var cornerRadius: CGFloat | |
init(origin: CGPoint, size: CGSize, cornerRadius: CGFloat) { | |
self.cornerRadius = cornerRadius | |
super.init(origin: origin, size: size) | |
} | |
} | |
class Contens { | |
var shapes: [Shape] | |
init(shapes: [Shape]) { | |
self.shapes = shapes | |
} | |
init?(binary: Data) { | |
// TODO: archive shapes to binary | |
fatalError() | |
} | |
func binary() -> Data { | |
// TODO: unarchive all shpes from binary | |
fatalError() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment