Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active September 4, 2021 07:48
Show Gist options
  • Save codelynx/428b27b3cfd58b8c7382346f1a4bc415 to your computer and use it in GitHub Desktop.
Save codelynx/428b27b3cfd58b8c7382346f1a4bc415 to your computer and use it in GitHub Desktop.
save/restore abstract objects
/*
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