Reputation: 7
I am currently learning about how delegates work, however I cannot understand why a variable is initialized as a type of the class, and not instantiated. How can a variable be a type of a class and why is that necessary when you could just instantiate the class?
var person: person!
Upvotes: 0
Views: 72
Reputation: 859
The statement referenced is a variable declaration without an initializer expression. The normal format is:
var variable name: type = expression
The colon followed by type is a type annotation, while the expression that follows the equal sign is the actual initialization expression.
var person: Person!
So here we are declaring a variable named person, annotated with a type of Person, and the exclamation point marks it as an implicitly unwrapped optional.
There are probably several reasons why a variable (whether class or struct or some other type) might be declared without also being initialized:
An example is when working with IBOutlet variables that are declared inside a view controller class. These variables are usually declared as implicitly unwrapped optionals. When the view controller object is first created, these variables are not yet initialized. However, they are always set by the time viewDidLoad is invoked.
Another reason might be that it just wouldn't make sense in terms of the application logic to instantiate the variable immediately. Lazy initialization is a special case of this where the variable in question is automatically initialized at the first point it is needed.
Upvotes: 0
Reputation: 437422
How can a variable be a type of a class and why is that necessary when you could just instantiate the class?
You obviously can just create an instance of the class:
var person = Person(...) // Note, class names generally begin with uppercase letter
But consider the following:
var person: Person!
This means that person
is now an Optional, one that can refer to a Person
instance. And the !
indicates that this optional will be implicitly unwrapped when you reference it in your code.
So the question is why would you use the latter (the implicitly unwrapped reference which is not yet set) rather than the former (just instantiating the Person
immediately). The answer is that you generally do this when the person
variable simply cannot be set initially, but will be set later.
A common example would be a “details” view controller which will show us details about a Person
object selected from a prior view controller. If this details view controller is instantiated from, say, a storyboard scene, clearly the person
variable isn’t set yet, so it has to be an optional. But the presenting view controller (or the coordinator or whatever) will set the person
reference after the scene’s view controller is first instantiated, but before the view controller appears on the screen. E.g., we might set the destination’s person
inside the presenting view controller’s prepare(for:sender:)
method.
So, in this case, we would declare person
to be an Optional (so the view controller can be instantiated, even though the person
hasn’t been set yet), but declare it to be an implicitly unwrapped one (Person!
) because we know the presenter will make sure to set person
before the details view controller appears on screen.
Upvotes: 1