Skip to content

Instantly share code, notes, and snippets.

Last active September 4, 2021 07:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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) { = 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
func binary() -> Data {
// TODO: unarchive all shpes from binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment