Chapter    42

De-initialization

When an instance is no longer need, Swift deallocates the instance and frees up the instance resources. For the most part, this is an automatic process that you don’t need to worry about when you are using standard Swift types. However, if you are using resources that need to be manually freed up (such as open files), you can de-initialize these resources by overriding the deinit method (see Listing 42-1).

Listing 42-1. deinit Method

class Person {    var name: String = "Name"    var age:Int = 0    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }    deinit {        //Remove any resources outside of standard        //types and objects here    }}

In Listing 42-1, you ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.