emreozdil / Swift-Daily-Tips

:octocat: Daily Tips from Swift World
MIT License
1.38k stars 79 forks source link

Builder pattern #35

Closed christophaigner closed 5 years ago

christophaigner commented 5 years ago

Description

The builder pattern allows you to create an instance and setting it up in a simple way.

Instead of writing e.g.

private let tableView: UITableView = {
    let tableView = UITableView()
    ... // setup code here
    return tableView
}

you can use the builder pattern and save 2 lines for every property you create.

private let tableView = UITableView(frame: .zero, style: .plain).with { tableView in
  ... // setup code here
}