Recall & Review
beginner
What is a deinitializer in Swift?
A deinitializer is a special method called automatically just before an instance of a class is deallocated. It is used to perform cleanup tasks like freeing resources.Click to reveal answer
beginner
How do you declare a deinitializer in a Swift class?
You declare a deinitializer using the keyword
deinit followed by curly braces. It has no parameters and no return type.<br><br>Example:<br>deinit {<br> // cleanup code<br>}Click to reveal answer
intermediate
When is the deinitializer called in Swift?
The deinitializer is called automatically when the last strong reference to a class instance is removed, just before the instance's memory is freed.Click to reveal answer
intermediate
Can structs or enums have deinitializers in Swift?
No, only classes can have deinitializers because structs and enums are value types and do not use reference counting.
Click to reveal answer
beginner
Why is it important to use deinitializers for cleanup?
Deinitializers help release resources like file handles, network connections, or observers to avoid memory leaks and keep the app efficient.
Click to reveal answer
Which keyword is used to define a deinitializer in Swift?
✗ Incorrect
The keyword
deinit is used to define a deinitializer in Swift.When is a deinitializer called?
✗ Incorrect
A deinitializer is called automatically just before an instance is deallocated.
Which Swift types can have deinitializers?
✗ Incorrect
Only classes can have deinitializers because they are reference types.
What is a common use of deinitializers?
✗ Incorrect
Deinitializers are used to clean up resources before an instance is destroyed.
Can you call a deinitializer manually in Swift?
✗ Incorrect
Deinitializers are called automatically by Swift when the instance is deallocated.
Explain what a deinitializer is and when it runs in Swift.
Think about what happens when an object is no longer needed.
You got /3 concepts.
Describe why only classes have deinitializers and not structs or enums.
Consider how memory management differs between these types.
You got /3 concepts.