Skip to content

Instantly share code, notes, and snippets.

@norman784
Created December 20, 2017 14:10
Show Gist options
  • Save norman784/a224f1c83382e8eeddc0f76ccf17b52f to your computer and use it in GitHub Desktop.
Save norman784/a224f1c83382e8eeddc0f76ccf17b52f to your computer and use it in GitHub Desktop.
Lazy way to initialize a class without having multiple constructors (only works with mutable vars)
class Demo {
var foo: String? = nil
var bar: String? = nil
init(block: (Demo) -> Void) {
block(self)
}
}
Demo {
$0.foo = "bar"
}
Demo {
$0.bar = "foo"
}
Demo {
$0.foo = "bar"
$0.bar = "foo"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment