3

Swift provides a deinit function for every object and I am wondering if Kotlin has an equivalent function

Here is how it works in Swift (Apple Doc):

A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how intializers are written with the init keyword. Deinitializers are only available on class types.

Typically you don’t need to perform manual clean-up when your instances are deallocated. However, when you are working with your own resources, you might need to perform some additional clean-up yourself. For example, if you create a custom class to open a file and write some data to it, you might need to close the file before the class instance is deallocated.

Community
  • 1
  • 1
Stanislas Heili
  • 295
  • 2
  • 10
  • 1
    You can override the `finalize` method. [Javadoc](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize()) – apetranzilla Dec 11 '18 at 18:15

0 Answers0