Back to feed
Dev.to
Dev.to
7/22/2026
Swift Classes โ€” Deinitializers and How Swift Cleans Up After Itself ๐Ÿงน

Swift Classes โ€” Deinitializers and How Swift Cleans Up After Itself ๐Ÿงน

Short summary

A beginner-friendly explanation of Swift's Automatic Reference Counting (ARC) and deinitializers. Classes use a hidden reference counter that increments on each new reference and decrements when references disappear; when it hits zero, Swift calls deinit automatically. Unlike init, deinit takes no parameters and cannot be called manually. The article includes code examples with anime-themed class instances to illustrate the lifecycle.

  • โ€ขARC tracks a hidden reference count per class instance โ€” when it hits zero, the instance is destroyed
  • โ€ขdeinit runs automatically when the last reference disappears, cannot be called manually
  • โ€ขStructs don't need ARC because each copy is independent data

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more