ios - accessing variables created in the override func viewDidLoad() { super.viewDidLoad() function outside that function in swift -
import uikit class secondviewcontroller: uiviewcontroller { @iboutlet weak var labela: uilabel! @iboutlet weak var labelb: uilabel! var datapassed:string! var seconddatapassed:string! var newvar: string! var newvar2: string! override func viewdidload() { super.viewdidload() labela.text = datapassed labelb.text = seconddatapassed newvar = labela.text println(newvar) } println(newvar) *** can't access newvar outside override func viewdidload() { - gives "expected declaration" driving me crazy!!!*** override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
is there data in datapassed? if don't assign string datapassed variable, , assign newvar datapassed, newvar have nothing print.
try:
import uikit class secondviewcontroller: uiviewcontroller { @iboutlet weak var labela: uilabel! @iboutlet weak var labelb: uilabel! var datapassed:string! = "test." var seconddatapassed:string! var newvar: string! var newvar2: string! override func viewdidload() { super.viewdidload() labela.text = datapassed labelb.text = seconddatapassed newvar = labela.text println(newvar) }
secondly, appears you're trying println
again outside of function. isn't going work, because viewdidload "main method" of app. can create other functions respond button touches, etc... , run println there, because swift code executed functionally, code you're executing has inside of particular function. while can declare variables, have, can't perform actions such printing them outside of function, because there's no order/method madness.
the place can run swift code on standalone basis without functions in swift playground. in xcode can select file -> new -> playground try out.
Comments
Post a Comment