Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active September 4, 2021 07:48

Revisions

  1. codelynx revised this gist Sep 4, 2021. 1 changed file with 4 additions and 12 deletions.
    16 changes: 4 additions & 12 deletions archive-unarchive-1.swift
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,6 @@
    import Foundation
    import CoreGraphics

    struct RGBA: Codable, CustomStringConvertible {
    var r: Float16
    var g: Float16
    var b: Float16
    var a: Float16
    var description: String { "(\(self.r), \(self.r), \(self.r), \(self.r))" }
    }

    protocol Shape {
    }

    @@ -35,10 +27,10 @@ class Rectangle: Shape {
    }
    }

    class ColoredRectangle: Rectangle {
    var color: RGBA
    init(origin: CGPoint, size: CGSize, color: RGBA) {
    self.color = color
    class RoundedRectangle: Rectangle {
    var cornerRadius: CGFloat
    init(origin: CGPoint, size: CGSize, cornerRadius: CGFloat) {
    self.cornerRadius = cornerRadius
    super.init(origin: origin, size: size)
    }
    }
  2. codelynx created this gist Sep 3, 2021.
    59 changes: 59 additions & 0 deletions archive-unarchive-1.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    /*
    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

    struct RGBA: Codable, CustomStringConvertible {
    var r: Float16
    var g: Float16
    var b: Float16
    var a: Float16
    var description: String { "(\(self.r), \(self.r), \(self.r), \(self.r))" }
    }

    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 ColoredRectangle: Rectangle {
    var color: RGBA
    init(origin: CGPoint, size: CGSize, color: RGBA) {
    self.color = color
    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()
    }
    }