Analysis process of the Swift programming language and the swift programming language process

Source: Internet
Author: User

Analysis process of the Swift programming language and the swift programming language process

The Destructor is called immediately before a class instance is released. Deinit is used to indicate the destructor, similar to the initialization function. The Destructor only applies to the class type.

 

 

Analysis Process Principle

Swift Automatically releases instances that are no longer needed to release resources. As described in the automatic reference count chapter, Swift processes the memory management of an instance through the automatic reference count (ARC. Generally, you do not need to manually clean up your instance when it is released. However, when using your own resources, you may need to perform some extra cleaning. 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 released.

 

In the definition of a class, each class can have at most one destructor. The Destructor does not contain any parameters, but does not contain parentheses:

 

Deinit {// execute the Destructor}


The Destructor is automatically called one step before the instance is released. You cannot actively call your own destructor. The subclass inherits the destructor of the parent class. At the end of the implementation of the subclass destructor, The destructor of the parent class is automatically called. Even if the subclass does not provide its own destructor, The destructor of the parent class is always called.

 

Because the instance is released only when the destructor of the instance is called, The Destructor can access the attributes of all request instances, you can modify the behavior of an object based on its attributes (for example, you can find the name of the object to be closed ).

 

 

Destructor

Here is an example of destructor operations. This example is a simple game that defines two new types: Bank and Player. The Bank Structure manages the circulation of a virtual currency, in which the Bank will never have more than 10,000 coins. In this game, only one Bank exists. Therefore, the Bank is implemented by a struct with static attributes and static methods to store and manage its current status.

 

struct Bank {   static var coinsInBank = 10_000   static func vendCoins(var numberOfCoinsToVend: Int) -> Int {         numberOfCoinsToVend = min(numberOfCoinsToVend, coinsInBank)         coinsInBank -= numberOfCoinsToVend        return numberOfCoinsToVend    }   static func receiveCoins(coins: Int) {   coinsInBank += coins    }}


The Bank tracks the number of coins it owns based on its coinsInBank attribute. The Bank also provides two methods-vendCoins and receiveCoins-to process coin distribution and collection.

 

The vendCoins method checks whether there are enough coins before the bank distributes the coins. If there are not enough coins, the Bank returns a number smaller than the request time (if there are no coins left in the bank, 0 is returned ). The vendCoins method declares numberOfCoinsToVend as a variable parameter, so that you can modify numbers within the method body without defining a new variable. The vendCoins method returns an integer value indicating the actual number of coins provided.

 

The receiveCoins method only adds the coins stored in the bank and the number of coins received, and then saves them back to the bank.

 

The Player class describes a Player in the game. Each player has a certain number of coins stored in their wallet at any time. This is reflected by the coinsInPurse attribute of player:

 

class Player {     var coinsInPurse: Int     init(coins: Int) {         coinsInPurse = Bank.vendCoins(coins)    }   func winCoins(coins: Int) {         coinsInPurse += Bank.vendCoins(coins)    }   deinit {         Bank.receiveCoins(coinsInPurse)    }    }

Each Player instance is initialized with a starting quota consisting of a specified number of coins obtained during bank initialization. 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, which obtains a certain number of coins from the bank and adds them to the Player's wallet. The Player class also implements a destructor called one step before the Player instance is released. Here, the Destructor only returns all the coins of the player to the bank:

 

Var playerOne: Player? = Player (coins: 100) println ("A new player has joined thegame with \ (playerOne !. CoinsInPurse) coins ") // output" A new playerhas joined the game with 100 coins "println (" There are now \ (Bank. coinsInBank) coins left in the bank ") // output" There arenow 9900 coins left in the bank"


A new Player instance is created with a 100 coin (if any) request. This Player instance is stored in an optional Player variable named playerOne. An optional variable is used because players can leave the game at any time. Set to optional so that you can track whether a player is currently in the game.

 

Because playerOne is optional, a exclamation point (!) When the winCoins method is called, The coinsInPurse attribute is accessed and the default number of coins is printed.

 

PlayerOne !. WinCoins (2_000) println ("PlayerOne won 2000 coins & now has \ (playerOne !. CoinsInPurse) coins ") // output" PlayerOnewon 2000 coins & now has 2100 coins "println (" The bank now only has \ (Bank. coinsInBank) coins left ") // output" The bank nowonly has 7900 coins left"


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

 

PlayerOne = nilprintln ("PlayerOne has left thegame") // output "PlayerOnehas left the game" println ("The bank now has \ (Bank. coinsInBank) coins ") // output" The bank nowhas 10000 coins"


Players have now left the game. This indicates that the optional playerOne variable must be set to nil, meaning "no Player instance ". In this case, the reference of the playerOne variable to the Player instance is damaged. There are no other attributes or variables that reference the Player instance. Therefore, to clear the memory occupied by the Player, release it. Before this happens, the Destructor is automatically called, and the coin is returned to the bank.


How to Use destructor in c language is not the content in c ++, but how to use destructor in c Language

C language is a process-oriented programming language, and constructor and destructor are used in object-oriented programming languages. So there is no destructor in C!

Swift Programming Language

Xcode used;

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.