uitableview - Why does my superclass call the subclass' method instead of its own in Swift? -
here's example project: http://cl.ly/3n2u2i1s441m
i'm in uitableviewcell
superclass, because when subclass being initiated call super.init()
. @ bottom of init
s both subclass , superclass, call method, call stylecell
applies styling it. method protocol both conform to, 1 implicitly conforming because it's subclass , overrides method.
at end of super class' init
, style method gets called, calls subclass' stylecell
method, not own.
why on earth happening?
is bug swift? i'm attaching code in addition project above show issue:
superclass table cell:
class supertableviewcell: uitableviewcell, style { var mysuperview: uiview! override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) mysuperview = uiview() dostyle() } required init?(coder adecoder: nscoder) { fatalerror("must created in code.") } func dostyle() { print("super class") } }
subclass table cell:
class subtableviewcell: supertableviewcell { var mysubview: uiview! override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) mysubview = uiview() dostyle() } required init?(coder adecoder: nscoder) { fatalerror("must created in code.") } override func dostyle() { super.dostyle() mysubview!.backgroundcolor = uicolor.greencolor() } }
style class , protocol:
class stylemanager: nsobject { } protocol style { func dostyle() }
this causes runtime error crash occurring when subclass cell tries set view in dostyle()
.
thought lot , think best illustration of going on putting in 1 "class".
by overriding dostyle
function replacing it. can still access super.
don't have way call super.
super class
, make point it's own method. makes sense because super , subclasses not 2 objects. 1 object 1 source of code 1 piece in super , in sub.
so when dostyle
function gets triggered super init
sees replaced/overridden method.
override
great @ doing name says does, overriding methods. implied don't use super.method , override method side side.
aside bad practice. use unique names unique functions (which have). in way makes sense use same name / protocol because in each function setting style. these not 2 separate classes both need style. setting basic style , specific style. 2 different things both need done.
class supersubillustation { // super , sub class 1 illustation init() { subinit() } func superinit() { print("super init") overridedostyle() // calls sub style because it's own method overridden } func subinit() { // sub init, overrides superinit print("sub init") superinit() // super.init() overridedostyle() // sub style } func dostyle() { print("super style") } func overridedostyle() { print("sub style") dostyle() // super.dostyle } }
side note :
when subclassing create increasingly more specific code. example series of uibutton classes.
class roundedcornersbutton : uibutton { override init(frame: cgrect) { super.init(frame: frame) self.layer.cornerradius = 5 } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) self.layer.cornerradius = 5 } } class greenbutton : roundedcornersbutton { override init(frame: cgrect) { super.init(frame: frame) self.backgroundcolor = uicolor.greencolor() } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) self.backgroundcolor = uicolor.greencolor() } } class redbutton : roundedcornersbutton { override init(frame: cgrect) { super.init(frame: frame) self.backgroundcolor = uicolor.redcolor() } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) self.backgroundcolor = uicolor.redcolor() } }
even though both init
, super.init
called, there no conflicts because subclass not call both it's own init
, it's super's init
.
Comments
Post a Comment