Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created May 4, 2016 18:41
Show Gist options
  • Save zwaldowski/41a6965820b66e60257788eb0b5c1d58 to your computer and use it in GitHub Desktop.
Save zwaldowski/41a6965820b66e60257788eb0b5c1d58 to your computer and use it in GitHub Desktop.
import UIKit
public protocol NibLoadable: class {
static var nibName: String { get }
static var nib: UINib { get }
}
extension NibLoadable {
public static var nibName: String {
let name = NSStringFromClass(self)
let start = name.rangeOfString(".", options: .BackwardsSearch)?.endIndex ?? name.startIndex
return String(name.characters.suffixFrom(start))
}
public static var nib: UINib {
return UINib(nibName: nibName, bundle: NSBundle(forClass: self))
}
public static func makeFromNib(@noescape configurator configure: Self throws -> ()) rethrows -> Self {
for case let view as Self in nib.instantiateWithOwner(nil, options: nil) {
try configure(view)
return view
}
preconditionFailure("Problem instantiating view from \(self).")
}
public static func makeFromNib() -> Self {
return makeFromNib { _ in }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment