Skip to content

Instantly share code, notes, and snippets.

@barbosa
Created June 12, 2017 18:56
Show Gist options
  • Select an option

  • Save barbosa/40839764a823242c73f8d8daa1a30952 to your computer and use it in GitHub Desktop.

Select an option

Save barbosa/40839764a823242c73f8d8daa1a30952 to your computer and use it in GitHub Desktop.
import UIKit
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(describing: self)
}
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(describing: self)
}
static func fromNib(_ nibNameOrNil: String? = nil) -> Self {
guard let instance = fromNib(self, nibNameOrNil: nibNameOrNil) else {
fatalError("Could not load nib with name: \(String(describing: nibNameOrNil))")
}
return instance
}
fileprivate static func fromNib<T>(_ type: T.Type, nibNameOrNil: String? = nil) -> T? {
return Bundle.main.loadNibNamed("\(self)", owner: nil, options: nil)?.first as? T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment