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

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -