ios - Swift : Access variable in ViewController -


i have view controller button, index, , function thats called when button pressed, here example of code:

view controller:

func buttonpressed(){  index++  } 

then have class in want access , print index view controller

class:

print("index \(viewcontroller().index)") 

obviously doesn't work, know how can access this? can't instantiate because viewcontroller not same index.i think.

you can save index nsuserdefaults or plist file. try using getter , setter make persist automatically follow:

xcode 8.3.3 • swift 3.1.1

extension userdefaults {     var indexa: int {         {             return integer(forkey: "indexa")         }         set {             set(newvalue, forkey: "indexa")         }     } } 

usage:

to load

let indexa = userdefaults.standard.indexa 

to set/change it

userdefaults.standard.indexa = 10 

Comments