Created
May 4, 2016 18:41
-
-
Save zwaldowski/41a6965820b66e60257788eb0b5c1d58 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 | |
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