ios - Returning Self in Swift -
aim: make generic viewcontroller , tableviewcontroller able return existing storyboards , subclassed other view controllers , allow them use functionality.
class generictableviewcontroller: uitableviewcontroller { //mark: storyboard class func storyboardname() -> string { return "" } class func storyboardidentifier() -> string { return "" } class func existingstoryboardcontrollertemplate() -> self { return uistoryboard.storyboardwithname(storyboardname()).instantiateviewcontrollerwithidentifier(storyboardidentifier()) as! self } }
the problem is.. compiler forces me change self "generictableviewcontroller" , if change it... complains no longer return "self".
is there can fix this?
doing following should work:
class func existingstoryboardcontrollertemplate() -> self { return existingstoryboardcontrollertemplate(self) } private class func existingstoryboardcontrollertemplate<t>(type: t.type) -> t { return uistoryboard(name: storyboardname(), bundle: nil).instantiateviewcontrollerwithidentifier(storyboardidentifier()) as! t }
basically create generic version of existingstoryboardcontrollertemplate
, add method compiler infer type of t
.
Comments
Post a Comment