Skip to content

Instantly share code, notes, and snippets.

@jaumevn
Last active November 16, 2017 14:07
Enumeration with two extensions
protocol Taskable {
func execute()
var description: String { get }
}
protocol Serializable {
func serialize()
}
enum LayerActions {
case scale(width: Double, height: Double)
case rotate(degrees: Double)
case fill(hex: String)
}
extension LayerActions: Taskable {
func execute() {
switch self {
case .scale(let width, let height):
print("Executes scale action")
case .rotate(let degrees):
print("Executes rotate action")
case .fill(let hexColor):
print("Executes fill action")
}
}
var description: String {
switch self {
case .scale(let width, let height):
return "Scales the layer to \(width)x\(height)"
case .rotate(let degrees):
return "Rotates the layer \(degrees) degrees"
case .fill(let hexColor):
return "Fills the layer with \(hexColor) color"
}
}
}
extension LayerActions: Serializable {
func serialize() {
// Code that serialize the enum into a json file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment