Skip to content

Instantly share code, notes, and snippets.

class MyClass: UIView {
let tableView: UITableView
let textField: UITextField
let buttonOne: UIButton
let buttonTwo: UIButton
init() {
tableView = UITableView(frame: .zero, style: .plain)
tableView.backgroundColor = .clear
class Foo {
var bar: String
init() { }
}
// Before
let foo = Foo()
foo.bar = "bar"
// After
infix operator <==
@discardableResult
public func <== <T>(x: T, f: (T) -> ()) -> T {
f(x)
return x
}
protocol HasModel {
associatedtype Model
var model: Model? { get set }
}
protocol HasHeight {
static var height: CGFloat { get }
}
extension UIView {
@objc protocol ViewDelegate {
func pressedButton()
}
class View: UIView {
let button = UIButton()
.stylePrimary(fontSize: 14)
.layout(width: 50, height: 30)
extension UIButton {
func stylePrimary(fontSize: CGFloat) -> UIButton {
// Apply styles e.g. round corners, border, font etc
setBackgroundImage(.init(with: .orange), for: .normal)
setBackgroundImage(.init(with: .darkOrange), for: .highlighted)
return self
}
}
let button = UIButton().stylePrimary(fontSize: 14)
// With segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case .some(“doTheThing”):
guard let vc = segue.destination as? ParameterizedViewController else { return }
vc.param = sender.param
}
}
// Without segues
@ConorBrady
ConorBrady / TableViewController.swift
Last active April 25, 2017 16:17
Generic table view controller
class TableViewController<Cell>: UITableViewController where Cell: UITableViewCell, Cell: HasModel, Cell: HasHeight {
let models: [Cell.Model]
let onSuccess: (Cell.Model, TableViewController<Cell>) -> ()
init(
models: [Cell.Model],
style: UITableViewStyle,
onSuccess: @escaping (Cell.Model, TableViewController<Cell>) -> ()
) {