A collection of extensions and methods to make Swift and iOS even better
Inspired by Soroush Khanlou's talk, You Deserve Nice Things, at #Pragma Conference 2017.
To load a view from a nib, don't use this:
let view = Bundle.main.loadNibNamed("YourView", owner: self, options: nil)?.first as? YourView
Instead, use this:
let view = YourView().loadNib()
Just grab UIView+loadNib.swift to make loading nibs super easy.
Thanks to Matt Lorentz for contributing this!
To dequeue a table view cell, don't use this:
dequeueReusableCell(withIdentifier identifier: String)
Instead, use this:
dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath)
Why? Look closely at the documentation for each, and check out the Return Value. On the second, it says:
A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
This is nicer than checking for nil and instantiating a new cell.