Consider the following code which loads a view based on a nib/xib with the same name as the class...
extension UIView
{
static func loadFromNib<T:UIView>() -> T
{
let nibName = String(T)
let bundle = NSBundle(forClass: self)
return bundle.loadNibNamed(nibName, owner: nil, options: nil)[0] as! T
}
}
In order to use it, you need to do this...
let view:SomeView = UIView.loadFromNib()
This way, T
is inferred to be SomeView
from the variable it's being assigned to.
However, I'm trying to get it down to this, if possible...
let view = SomeView.loadFromXib()
...where the type SomeView
is inferred from the class loadFromNib
is called on, but I can't figure out how to define the constraint for T
relative to self
to support that.
So is this possible?