Skip to content

Instantly share code, notes, and snippets.

@pbrewczynski
Created February 20, 2017 09:41
Show Gist options
  • Save pbrewczynski/3f7b8396ccc06b32ccc3e1ee99e17672 to your computer and use it in GitHub Desktop.
Save pbrewczynski/3f7b8396ccc06b32ccc3e1ee99e17672 to your computer and use it in GitHub Desktop.
protocol UIViewLoading {}
extension UIView : UIViewLoading {}
extension UIViewLoading where Self : UIView {
// note that this method returns an instance of type `Self`, rather than UIView
static func loadFromNib() -> Self {
let nibName = "\(self)".characters.split{$0 == "."}.map(String.init).last!
let nib = UINib(nibName: nibName, bundle: nil)
return nib.instantiate(withOwner: self, options: nil).first as! Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment