0
0
Swiftprogramming~5 mins

Deinitializers for cleanup in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Adeinit
Binit
Cdestroy
Dcleanup
When is a deinitializer called?
AWhen the instance is created
BWhen a method is called
CWhen the program starts
DWhen the instance is about to be deallocated
Which Swift types can have deinitializers?
AClasses only
BStructs only
CEnums only
DClasses, structs, and enums
What is a common use of deinitializers?
ATo initialize properties
BTo create new instances
CTo clean up resources like files or network connections
DTo define methods
Can you call a deinitializer manually in Swift?
AYes, anytime
BNo, it is called automatically
COnly inside the class
DOnly from subclasses
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.