Last active
January 21, 2016 16:55
-
-
Save yas375/b494eea71d42e562705e 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 | |
protocol NibLoadable: class { | |
typealias Object | |
static var nibName: String { get } | |
static var nib: UINib { get } | |
static var firstObjectFromNib: AnyObject? { get } | |
} | |
extension NibLoadable { | |
static var nibName: String { | |
return NSStringFromClass(self).componentsSeparatedByString(".").last! | |
} | |
static var nib: UINib { | |
return UINib(nibName: nibName, bundle: nil) | |
} | |
static var firstObjectFromNib: AnyObject? { | |
return nib.instantiateWithOwner(nil, options: nil).first | |
} | |
} | |
// Example | |
class EmptyStateView: UIView { | |
// this is really optional | |
static func view() -> EmptyStateView { | |
return EmptyStateView.firstObjectFromNib! | |
} | |
} | |
extension EmptyStateView: NibLoadable { | |
typealias Object = EmptyStateView | |
} | |
let view = EmptyStateView.view() | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment