How to call deinit in Swift [duplicate]

The problem is that a playground is not real life. This is just one more reason for not using them (I think they are a terrible mistake on Apple’s part). Use a real iOS app project and deinit will be called as expected.

Example from a real project:

class ViewController: UIViewController {
    class Person{
        let name:String;
        init(name:String){
            self.name = name;
            println("\(name) is being initialized.");
        }
        deinit{
            println("\(name) is being deInitialized.");

        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        var person:Person?;
        person = Person(name:"leo");
        person = nil;
    }
}

That does what you expect it to do.

Leave a Comment