Skip to content

Instantly share code, notes, and snippets.

@pofat
Created May 22, 2019 13:40
Show Gist options
  • Save pofat/80344edd533ac6b38a58eae6b54fc2c6 to your computer and use it in GitHub Desktop.
Save pofat/80344edd533ac6b38a58eae6b54fc2c6 to your computer and use it in GitHub Desktop.
// From:
protocol Drawable {
func draw()
}
// To:
struct Drawing<Shape> {
var draw: (Shape) -> ()
}
// 用來畫實心點
let solidPointDrawer = Drawing<CGPoint> { point in
print("draw solid point: (\(point.x), \(point.y))")
}
// 用來畫空心點
let hollowPointDrawer = Drawing<CGPoint> { point in
print("draw hollow point: (\(point.x), \(point.y))")
}
let point = CGPoint(x: 1, y: 1)
solidPointDrawer.draw(point) // 畫實心點
hollowPointDrawer.draw(point) // 畫空心點
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment