If I have a class and initialize a variable like so:
class TestClass: UIViewController {
var thisInt: Int = 10
}
is that any different than initializing like so:
class TestClass: UIViewController {
var thisInt: Int!
override func viewDidLoad() {
super.viewDidLoad()
thisInt = 10
}
}
I suppose my main questions lie in when the global initialization takes place, and is there a time when one would get called more than the other would be called with normal iOS Programming (not doing anything drastically against native development). I do understand that doing it in viewDidLoad restricts me to using weak or an optional, but I'm more concerned about any other differences.
You can find out easily by adding a "computed" property to your view controller:
and adding a
in app's delegate
didFinishLaunchingWithOptions
method.The order you'll get is
which means global initializers run before the app lifecycle begins.
Now, to go even further, you could add a
main.swift
file with the following contentsand remove (or comment) the
@UIApplicationMain
decoration from your AppDelegate class. This will instruct the compiler to use the code inmain.swift
for the application initialization instead of the default behavior provided by the decorator in discussion (although we're providing a custom almost identical implementation).What you'll get in this 2nd approach is
which means that the instance property code is executed when the storyboard is loaded.
Now, for more insight, let's try to find out the differences between static an instance variables. For this we'll add a test class:
, and update AppDelegate's did finish launching:
The output we get is:
, which confirms the fact that instance properties get set when the class is instantiated, and before any initializer code runs.
But wait! What about the static property? There are no traces to indicate that it was initialized at all. Looks like static properties are lazy by definition, and get initialized only when accessed the first time.
Updating the app did finish launching code confirms this.
gives the following output:
To conclude: