swift - Change static variable -
im new in swift. i'd ask u if go right way.
i have this:
class viewcontroller: uiviewcontroller { struct myvars { var static short_text = "" } override func viewdidload() { super.viewdidload() loaddata() println(short_text) } func loaddata() { myvars.short_text = "hello world!" } }
this code works. have "hello world!" string in variable , can print it. iam asking if , clear way redefine static var? because want work variable across code.
thank answers.
ps: final methods more difficult. shorted code example only.
if intent make variable instance of viewcontroller
accessible other classes (i.e. other view controllers), don't have use static
. need use static
if critical make property accessible across multiple instances of class. don't think that's intended here.
if intent pass data between view controllers, i'd suggest refer to:
if search "pass data between view controllers", you'll find lots of other similar links.
bottom line, use of static
possible, not intended.
two side notes:
you using
static
withinstruct
. swift 1.2 obviates need pattern. if needstatic
(and don't think need here), can declare variablestatic
, eliminatestruct
:static var shorttext = ""
i don't think use of
struct
meant open "by-value vs by-reference" discussion, if did, might refer wwdc 2015 video building better apps value types in swift.
Comments
Post a Comment