Created
September 5, 2018 09:32
-
-
Save sgl0v/ccc213c0ef65c8687419a59375bd5ad3 to your computer and use it in GitHub Desktop.
NibLoadable protocol
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 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