Skip to main content

What does a deinitializer do?

· One min read
Ace the iOS Interview
Aryaman Sharda
Sources & Resources

Main Source: đź”— Ace the iOS Interview

Additional Sources:

Further Reading:

TL/DR

A deinitializer is a function that is called right before aclassis deallocated. Deinitializers are only available onclasstypes and eachclasscan onlyhave one deinitializer. This function does not accept any parameters.

You create a deinitializer using the deinit keyword:

deinit {
// Perform the deinitialization
}

Although Swift automatically deallocates instances when they are no longer needed (the retain count becomes 0), deinitializers allow you to perform additional cleanup before your instance is deallocated.

For example, you may want to invalidate timers, terminate a socket connection, or close out a connection to a database:

deinit {
database.closeConnection()
}

deinit {
timer?.invalidate()
timer = nil
}

Just before releasing an instance, the system will call the deinitializer automatically; do not call it yourself.