0Pricing
Swift Academy · Lesson

Deinitializers and Memory Lifecycle

Using deinit to clean up resources before an object is deallocated.

Welcome

Swift classes can define a `deinit` that runs just before the instance is deallocated. Understanding the full object lifecycle — init → use → deinit — is key to resource management.

What is deinit?

```swift class FileHandle { let path: String init(path: String) { self.path = path; print("Opened \(path)") } deinit { print("Closed \(path)") } } do { let f = FileHandle(path: "/tmp/log.txt") } // deinit called here when f goes out of scope ```

All lessons in this course

  1. Class Inheritance and the super Keyword
  2. override, final and Preventing Subclassing
  3. Type Casting: is, as?, as! and as
  4. Deinitializers and Memory Lifecycle
← Back to Swift Academy