Skip to content

Instantly share code, notes, and snippets.

@sgl0v
Created September 5, 2018 09:32
Show Gist options
  • Save sgl0v/ccc213c0ef65c8687419a59375bd5ad3 to your computer and use it in GitHub Desktop.
Save sgl0v/ccc213c0ef65c8687419a59375bd5ad3 to your computer and use it in GitHub Desktop.
NibLoadable protocol
import UIKit
protocol NibLoadable where Self: UIView {
static func viewFromNib() -> Self
}
extension NibLoadable {
static func viewFromNib() -> Self {
let nibName = String(describing: self)
guard let view = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)?.first as? Self else {
fatalError("Failed to load \(self) from nib file with name `\(nibName)`")
}
return view
}
}
extension UIView: NibLoadable { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment