iOS development language Swift Getting started serial---destruction process

Source: Internet
Author: User

iOS development language Swift Getting started serial-destruction process

Destructors are called immediately before an instance of a class is freed. The destructor is labeled with the keyword Deinit, similar to the initialization function, which is marked with init. Destructors apply only to class types.
  

Principle of the process of destruction

Swift frees up resources by automatically releasing instances that are no longer needed. As described in the Chapter automatic reference count, Swift handles memory management of instances by automatic reference counting (ARC). Typically, you do not need to manually clean up your instances when they are released. However, when you use your own resources, you may need to do some extra cleanup. For example, if you create a custom class to open a file and write some data, you may need to close the file before the class instance is freed.
In the definition of a class, there can be at most one destructor per class. Destructors do not have any arguments, and are not written in parentheses:

deinit {    // 执行析构过程}

A destructor is called automatically before the instance release occurs. You are not allowed to invoke your own destructor actively. Subclasses inherit the destructor of the parent class, and at the end of the subclass destructor implementation, the parent class's destructor is automatically called.  Destructors for a parent class are always called, even if the subclass does not provide its own destructor.  Because the instance is not freed until the instance's destructor is called, the destructor can access the properties of all the request instances, and it can modify its behavior based on those properties (such as finding a file name that needs to be closed). destructor operation here is an example of a destructor operation. This example is a simple game that defines two new types, Bank and player. The bank structure manages the flow of a virtual currency, in which the bank can never have more than 10,000 coins. There is only one bank in this game, so the bank is implemented by a struct with static attributes and static methods to store and manage its current state.

struct  Bank {static  var  coinsinbank = 10  _000 static  func vendcoins (var  Numberofcoinstovend:int), Int {numberofcoinstovend = min (numberofcoinstovend, Coinsinbank) coinsi Nbank-= numberofcoinstovend return  numberofcoinstovend} static  func receivecoins (coins:int) {Coinsinbank + = coins}}  

The

Bank tracks the number of coins it currently owns based on its Coinsinbank property.  The bank also provides two methods--vendcoins and receivecoins--to handle the distribution and collection of coins. The Vendcoins method checks if there are enough coins before the bank distributes the coins. If there are not enough coins, the bank returns a number that is smaller than the request (returns 0 if no coin is left in the bank). The Vendcoins method declares numberofcoinstovend as a variable argument so that the number can be modified inside the method body without having to define a new variable.  The Vendcoins method returns an integer value indicating the actual number of coins provided.  The Receivecoins method simply adds the number of coins stored and received to the Bank and saves it back to the bank. The player class describes a player in the game. Each player has a certain number of coins stored in their wallets at any one time. This is reflected by the player's Coinsinpurse property:

class  player  {var coinsinpurse: int  init ( Coins : int )  {coinsinpurse = bank . Vendc Oins (coins ) } func wincoins (coins : int )  {coinsinpurse + = bank . Vendcoins (coins ) } deinit {bank . Receivecoins (
      
       coinsinpurse 
      ) }}   

Each player instance is initialized with a specified number of coins, which are obtained during the bank initialization process.  If there are not enough coins available, the Player instance may receive fewer coins than the specified number. The player class defines a Wincoins method that obtains a certain number of coins from the bank and adds them to the player's wallet. The player class also implements a destructor, which is called in the previous step of the player instance release. Here the destructor simply returns all the players ' coins to the bank:

var playerOne: Player? = Player(coins: 100)println("A new player has joined the game with \(playerOne!.coinsInPurse) coins")// 输出 "A new player has joined the game with 100  coins"println("There are now \(Bank.coinsInBank) coins left   in the bank")// 输出 "There are now 9900 coins left in the bank"

A new player instance is created with a request of 100 coins (if any). This player instance is stored in an optional player variable named Playerone. An optional variable is used here because the player can leave the game at any time.  Set to optional so that you can track whether there are currently players in the game. Because Playerone is optional, it is made up of an exclamation point (! ) to decorate, whenever its Wincoins method is called, the Coinsinpurse property is accessed and prints out its default number of coins.

playerOne!.winCoins(2_000)println("PlayerOne won 2000 coins & now has \(playerOne!.coinsInPurse) coins")// 输出 "PlayerOne won 2000 coins & now has 2100 coins"println("The bank now only has \(Bank.coinsInBank) coins left")// 输出 "The bank now only has 7900 coins left"

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

nilprintln("PlayerOne has left the game")// 输出 "PlayerOne has left the game"println("The bank now has \(Bank.coinsInBank) coins")// 输出 "The bank now has 10000 coins"

The player has now left the game. This indicates that you want to set the optional Playerone variable to nil, which means "there is no player instance." When this happens, the Playerone variable's reference to the player instance is broken. There is no other property or variable that references the player instance, so it is freed in order to empty the memory it occupies. Before this occurs, its destructor is automatically called and its coins are returned to the bank.

iOS development language Swift Getting started serial---destruction process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.