ios - Swift Override class variables -



ios - Swift Override class variables -

i know reply has been posted in other form here, i'd understand more overriding instance variables in swift.

let's have code

class baseview:uiview{ allow somevariable:int = 1 // work somevariable } class extendedview:baseview{ allow somevariable:int = 2 }

ok. read, constant requires override prefix. other answers said should declare setter , getter? why? don't care two. need value replaced. can't utilize init override because i'm inheriting uiview , might quite unsafe (i think).

any suggestions welcomed.

as say, cannot redefine constant in subclass (it constant, after all). error "cannot override stored property". appear possible override var, however, when alter let somevariable var somevariable "ambiguous utilize of 'somevariable'" when access in subclass (note - same thing happens whether utilize override or not).

the simplest solution, initial advisors suggested, utilize getter. function, can happily override it, backing variable managed you, , if don't supply setter, constant:

class baseview: uiview { var somevariable: int { { homecoming 1 } } // work somevariable } class extendedview: baseview { override var somevariable: int { { homecoming 2 } } } allow = baseview() a.somevariable // 1 allow b = extendedview() b.somevariable // 2

ios class variables inheritance swift

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -