Created
September 27, 2016 14:51
-
-
Save sammoore/fd471eebb6ca8ef2b031e7381e4c5411 to your computer and use it in GitHub Desktop.
Untested Generic Nib Loader
This file contains 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
extension UINib { | |
static func Build<View: UIView>(nibName: String? = nil, bundle: NSBundle? = nil) -> View { | |
let typeName = String(Mirror(reflecting: View).subjectType) | |
let nibName: String = nibName ?? typeName | |
let nib = UINib(nibName: nibName, bundle: bundle) | |
for object in nib.instantiateWithOwner(nil, options: nil) { | |
if let view = object as? View { | |
return view | |
} | |
} | |
preconditionFailure("View of type \(typeName) not found in nib \(nibName)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment