Skip to content

Instantly share code, notes, and snippets.

@crowjdh
Last active September 5, 2015 04:11
Show Gist options
  • Save crowjdh/e0ec0a57329d2ee57b88 to your computer and use it in GitHub Desktop.
Save crowjdh/e0ec0a57329d2ee57b88 to your computer and use it in GitHub Desktop.
Simple builder pattern for Swift
import Foundation
// Usage:
//
// let car = Car(engine: "Awesome Engine", gas: 90, builder: {
// $0.hasCooler = true
// $0.hasSmartKey = true
// })
final class Car {
let engine: String
var gas: Int
var hasCooler: Bool?
var hasSmartKey = false
typealias CarBuilder = (Car) -> ()
init(engine: String, gas: Int, builder: CarBuilder) {
self.engine = engine
self.gas = gas
self.hasCooler = false
builder(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment