Hi, i'm a beginner to swift. I'm a bit confused by the positions of variable declarations within view controllers. Shoulf they always go with the other IBOutlet's, or do they go in the viewdidload() method?
Variables defined in
viewDidLoad() are local to that method, whereas the variables defined outside of any methods are part of the object itself. So the important part is, do you need to keep these variables around for a while or do you just need them for initial setup in
Also, this should probably in Using Swift and not Evolution. It's probably fine on this forum since it's really more of a general question than specifically about
Hi Mike, first, questions like this one should go in the “Using Swift” section, although your question is about iOS Development, not Swift per se. Please check the categories descriptions.
Answering your questions,
IBOutlet is just a marker that Xcode uses to know that var is related to something in your Storyboard.
Variables and constant defined inside your
ViewController are properties of that class, you can use them inside your class, remember, your
ViewController is like any other class, and
viewDidLoad is a function as any other, so, if you define a var inside
viewDidLoad, it will only be visible to that function domain.
ok thanks - its a little clearer now.
So being as viewdidload is executed only the once - it probably makes sense to make most variables , variables of the class ,to make them useful. right?
p.s. sorry for posting in the wrong area!