Deinitialization (析构过程)

Adeinitializeris called immediately before a class instance is deallocated. You write deinitializers with thedeinitkeyword, similar to how initializers are written with theinitkeyword. Deinitializers are only available on class types.

析构器只适用于类类型,当一个类的实例被释放之前,析构器会被立即调用。析构器用关键字deinit来标示,类似于构造器要用init来标示。

How Deinitialization Works (析构过程原理)

Swift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances throughautomatic reference counting(ARC), as described inAutomatic Reference Counting. Typically you don’t need to perform manual cleanup when your instances are deallocated. However, when you are working with your own resources, you might need to perform some additional cleanup 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.

Swift 会自动释放不再需要的实例以释放资源。如自动引用计数章节中所讲述,Swift 通过自动引用计数(ARC)处理实例的内存管理。通常当你的实例被释放时不需要手动地去清理。但是,当使用自己的资源时,你可能需要进行一些额外的清理。例如,如果创建了一个自定义的类来打开一个文件,并写入一些数据,你可能需要在类实例被释放之前手动去关闭该文件。

Class definitions can have at most one deinitializer per class. The deinitializer does not take any parameters and is written without parentheses:

在类的定义中,每个类最多只能有一个析构器,而且析构器不带任何参数,如下所示:

deinit {

// perform the deinitialization

}

Deinitializers are called automatically, just before instance deallocation takes place. You are not allowed to call a deinitializer yourself. Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation. Superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.

析构器是在实例释放发生前被自动调用。你不能主动调用析构器。子类继承了父类的析构器,并且在子类析构器实现的最后,父类的析构器会被自动调用。即使子类没有提供自己的析构器,父类的析构器也同样会被调用。

Because an instance is not deallocated until after its deinitializer is called, a deinitializer can access all properties of the instance it is called on and can modify its behavior based on those properties (such as looking up the name of a file that needs to be closed).

因为直到实例的析构器被调用后,实例才会被释放,所以析构器可以访问实例的所有属性,并且可以根据那些属性可以修改它的行为(比如查找一个需要被关闭的文件)。

Deinitializers in Action (析构器实践)

Here’s an example of a deinitializer in action. This example defines two new types,BankandPlayer, for a simple game. TheBankclass manages a made-up currency, which can never have more than 10,000 coins in circulation. There can only ever be oneBankin the game, and so theBankis implemented as a class with type properties and methods to store and manage its current state:

这是一个析构器实践的例子。这个例子描述了一个简单的游戏,这里定义了两种新类型,分别是Bank和Player。Bank类管理一种虚拟硬币,确保流通的硬币数量永远不可能超过 10,000。在游戏中有且只能有一个Bank存在,因此Bank用类来实现,并使用类型属性和类型方法来存储和管理其当前状态。

class Bank{

    static var coinsInBank=10_000

    static func distribute(coinsnumberOfCoinsRequested:Int) ->Int{

    let numberOfCoinsToVend=min(numberOfCoinsRequested,coinsInBank)

    coinsInBank-=numberOfCoinsToVend

    return numberOfCoinsToVend

}

static func receive(coins:Int) {

coinsInBank+=coins

 }

}

Bankkeeps track of the current number of coins it holds with itscoinsInBankproperty. It also offers two methods—distribute(coins:)andreceive(coins:)—to handle the distribution and collection of coins.

Bank使用coinsInBank属性来跟踪它当前拥有的硬币数量。Bank还提供了两个方法,distribute(coins:)和receive(coins:),分别用来处理硬币的分发和收集。

Thedistribute(coins:)method checks that there are enough coins in the bank before distributing them. If there are not enough coins,Bankreturns a smaller number than the number that was requested (and returns zero if no coins are left in the bank). It returns an integer value to indicate the actual number of coins that were provided.

distribute(coins:)方法在Bank对象分发硬币之前检查是否有足够的硬币。如果硬币不足,Bank对象会返回一个比请求时小的数字(如果Bank对象中没有硬币了就返回0)。此方法返回一个整型值,表示提供的硬币的实际数量。

Thereceive(coins:)method simply adds the received number of coins back into the bank’s coin store.

receive(coins:)方法只是将Bank实例接收到的硬币数目加回硬币存储中。

ThePlayerclass describes a player in the game. Each player has a certain number of coins stored in their purse at any time. This is represented by the player’scoinsInPurseproperty:

Player类描述了游戏中的一个玩家。每一个玩家在任意时间都有一定数量的硬币存储在他们的钱包中。这通过玩家的coinsInPurse属性来表示:

class Player{

    var coinsInPurse:Int

    init(coins:Int) {

    coinsInPurse=Bank.distribute(coins:coins)

}

func win(coins:Int) {

 coinsInPurse+=Bank.distribute(coins:coins)

}

deinit{

    Bank.receive(coins:coinsInPurse)

 }

}

EachPlayerinstance is initialized with a starting allowance of a specified number of coins from the bank during initialization, although aPlayerinstance may receive fewer than that number if not enough coins are available.

每个Player实例在初始化的过程中,都从Bank对象获取指定数量的硬币。如果没有足够的硬币可用,Player实例可能会收到比指定数量少的硬币.

ThePlayerclass defines awin(coins:)method, which retrieves a certain number of coins from the bank and adds them to the player’s purse. ThePlayerclass also implements a deinitializer, which is called just before aPlayerinstance is deallocated. Here, the deinitializer simply returns all of the player’s coins to the bank:

Player类定义了一个win(coins:)方法,该方法从Bank对象获取一定数量的硬币,并把它们添加到玩家的钱包。Player类还实现了一个析构器,这个析构器在Player实例释放前被调用。在这里,析构器的作用只是将玩家的所有硬币都返还给Bank对象:

var playerOne:Player? =Player(coins:100)

print("A new player has joined the game with\(playerOne!.coinsInPurse)coins")

// Prints "A new player has joined the game with 100 coins"

print("There are now\(Bank.coinsInBank)coins left in the bank")

// Prints "There are now 9900 coins left in the bank"

A newPlayerinstance is created, with a request for 100 coins if they are available. ThisPlayerinstance is stored in an optionalPlayervariable calledplayerOne. An optional variable is used here, because players can leave the game at any point. The optional lets you track whether there is currently a player in the game.

创建一个Player实例的时候,会向Bank对象请求 100 个硬币,如果有足够的硬币可用的话。这个Player实例存储在一个名为playerOne的可选类型的变量中。这里使用了一个可选类型的变量,因为玩家可以随时离开游戏,设置为可选使你可以追踪玩家当前是否在游戏中。

Because playerOne is an optional, it is qualified with an exclamation mark (!) when itscoinsInPurseproperty is accessed to print its default number of coins, and whenever itswinCoins(_:)method is called:

因为playerOne是可选的,所以访问其coinsInPurse属性来打印钱包中的硬币数量时,使用感叹号(!)来解包:

playerOne!.win(coins:2_000)

print("PlayerOne won 2000 coins & now has\(playerOne!.coinsInPurse)coins")

// Prints "PlayerOne won 2000 coins & now has 2100 coins"

print("The bank now only has\(Bank.coinsInBank)coins left")

// Prints "The bank now only has 7900 coins left"

Here, the player has won 2,000 coins. The player’s purse now contains 2,100 coins, and the bank has only 7,900 coins left.

这里,玩家已经赢得了 2,000 枚硬币,所以玩家的钱包中现在有 2,100 枚硬币,而Bank对象只剩余 7,900 枚硬币。

playerOne=nil

print("PlayerOne has left the game")

// Prints "PlayerOne has left the game"

print("The bank now has\(Bank.coinsInBank)coins")

// Prints "The bank now has 10000 coins"

The player has now left the game. This is indicated by setting the optionalplayerOnevariable tonil, meaning “noPlayerinstance.” At the point that this happens, theplayerOnevariable’s reference to thePlayerinstance is broken. No other properties or variables are still referring to thePlayerinstance, and so it is deallocated in order to free up its memory. Just before this happens, its deinitializer is called automatically, and its coins are returned to the bank.

玩家现在已经离开了游戏。这通过将可选类型的playerOne变量设置为nil来表示,意味着“没有Player实例”。当这一切发生时,playerOne变量对Player实例的引用被破坏了。没有其它属性或者变量引用Player实例,因此该实例会被释放,以便回收内存。在这之前,该实例的析构器被自动调用,玩家的硬币被返还给银行。

你可能感兴趣的:(Deinitialization (析构过程))