Swift Programming Tutorial: Deinitialization

Quickly Understand Deinitialization, Also Known as ‘Deinit’

Arc Sosangyo
3 min readNov 12, 2024
Photo by Manki Kim on Unsplash

Imagine you’re packing up your desk after a big project is done. You’ve completed your mission, high-fived your teammates, and now it’s time to clear out so you can walk off into the sunset (or power down in peace). That, in programming terms, is basically what deinitialization is all about.

This article is part of my Swift Programming Tutorial series. If you’re just beginning to learn Swift programming, make sure you understand classes first.

When you create an instance of a class in Swift, you allocate memory to store all its properties, methods, and any other resources it needs. But when that instance has served its purpose and is no longer needed, Swift has to deinitialize it — which is fancy talk for “cleaning up after itself.” This is where the deinit method comes into play.

Here’s the key idea: The deinit method in Swift is like the "goodbye" function for class instances. It’s called automatically just before an instance is zapped from memory. You don’t call deinit directly; Swift handles it when the instance goes out of scope (that’s tech talk for “nobody’s pointing to this instance anymore, so let’s clear the desk”).

--

--

Arc Sosangyo
Arc Sosangyo

Written by Arc Sosangyo

Arc is an iOS Dev and app publisher, a former IT manager who transitioned to iOS engineering, and a big fan of AI, coding, science, history, and philosophy.

No responses yet