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:

  1. you using static within struct. swift 1.2 obviates need pattern. if need static (and don't think need here), can declare variable static , eliminate struct:

    static var shorttext = "" 
  2. 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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -