Created
June 12, 2017 18:56
-
-
Save barbosa/40839764a823242c73f8d8daa1a30952 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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