Complete the code to define a deinitializer in a Swift class.
class FileHandler { init() { print("File opened") } [1] { print("File closed") } }
The deinit keyword defines a deinitializer in Swift, which runs when an instance is about to be deallocated.
Complete the code to print a message when the object is deallocated.
class Connection { init() { print("Connected") } deinit { [1]("Disconnected") } }
The print function outputs text to the console in Swift.
Fix the error in the deinitializer syntax.
class Logger { [1] { print("Logger destroyed") } }
The deinitializer in Swift is declared with deinit keyword without parentheses or 'func'.
Fill both blanks to create a class that opens a file and closes it when the object is destroyed.
class FileManager { init() { [1]("File opened") } [2] { print("File closed") } }
The init method runs when the object is created, and deinit runs when it is destroyed. Use print to show messages.
Fill all three blanks to create a class that tracks resource allocation and cleans up properly.
class Resource { var id: Int init(id: Int) { self.id = id [1]("Resource \(id) allocated") } [2] { [3]("Resource \(id) released") } }
Use print to show messages, init to initialize, and deinit to clean up when the object is destroyed.