Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created September 27, 2016 14:51
Show Gist options
  • Save sammoore/fd471eebb6ca8ef2b031e7381e4c5411 to your computer and use it in GitHub Desktop.
Save sammoore/fd471eebb6ca8ef2b031e7381e4c5411 to your computer and use it in GitHub Desktop.
Untested Generic Nib Loader
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