Skip to content

Instantly share code, notes, and snippets.

@ocwang
Created August 13, 2016 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocwang/42f61358f947f4a24cceea2967d1bf60 to your computer and use it in GitHub Desktop.
Save ocwang/42f61358f947f4a24cceea2967d1bf60 to your computer and use it in GitHub Desktop.
import UIKit
extension UIView {
class func toString() -> String {
let name = NSStringFromClass(self)
let components = name.componentsSeparatedByString(".")
guard let classString = components.last
else { fatalError("") }
return classString
}
class func ocw_nibNamed(nibName: String) -> UINib {
return UINib(nibName: nibName, bundle: NSBundle.mainBundle())
}
class func ocw_nib() -> UINib {
return ocw_nibNamed(self.toString())
}
class func ocw_instantiateWithNibNamed(nibName: String) -> AnyObject {
let nib = ocw_nibNamed(nibName)
let objects = nib.instantiateWithOwner(nil, options: nil)
guard let object = objects.first else {
fatalError("Error: couldn't create nib named \(nibName)")
}
return object
}
class func ocw_instantiateFromNib() -> AnyObject {
return ocw_instantiateWithNibNamed(self.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment