Swift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in Automatic Reference Counting.
Deinitializers in Action, a sample of Bank operations
structBank{staticvarcoinsInBank=10_000staticfuncvendCoins(varnumberOfCoinsToVend:Int)->Int{numberOfCoinsToVend=min(numberOfCoinsToVend,coinsInBank)coinsInBank-=numberOfCoinsToVendreturnnumberOfCoinsToVend}staticfuncreceiveCoins(coins:Int){coinsInBank+=coins}}classPlayer{varcoinsInPurse:Intinit(coins:Int){coinsInPurse=Bank.vendCoins(coins)}funcwinCoins(coins:Int){coinsInPurse+=Bank.vendCoins(coins)}deinit{Bank.receiveCoins(coinsInPurse)}}varplayerOne:Player?=Player(coins:100)println("A new player has joined the game with \(playerOne!.coinsInPurse) coins")// prints "A new player has joined the game with 100 coins"println("There are now \(Bank.coinsInBank) coins left in the bank")// prints "There are now 9900 coins left in the bank"playerOne!.winCoins(2_000)println("PlayerOne won 2000 coins & now has \(playerOne!.coinsInPurse) coins")// prints "PlayerOne won 2000 coins & now has 2100 coins"println("The bank now only has \(Bank.coinsInBank) coins left")// prints "The bank now only has 7900 coins left"playerOne=nilprintln("PlayerOne has left the game")// prints "PlayerOne has left the game"println("The bank now has \(Bank.coinsInBank) coins")// prints "The bank now has 10000 coins"