简体   繁体   中英

When should I use deinit?

我在阅读The Swift Programming Language guide时遇到了一个名为deinit()的函数,但我仍然想知道为什么以及何时需要实现它,因为我们实际上并不需要管理内存。

It's not required that you implement that method, but you can use it if you need to do some action or cleanup before deallocating the object.

The Apple docs include an example:

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
    }
}

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

So whenever the player is removed from the game, its coins are returned to the bank.

A deinit() is called immediately before a class instance is deallocated, and it is helpful when you are working with your own resources. For example, if you create a custom class to open a file and write some data to it, you might need close the file before the class instance is deallocated. The most important thing to remember is a class definition can have at most one deinit() per class

从 iOS9 开始,removeObserver 被自动调用。

如果您的类管理文件句柄或不同的资源,您可以在 deinit 中关闭该句柄,以确保在释放对象后它不会继续存在。

A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types.Class definitions can have at most one deinitializer per class. The deinitializer does not take any parameters and is written without parentheses. I used deinit to removeObserver of notification from the application,as given below.

deinit {
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoLogin"), object: nil)
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoMain"), object: nil)
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoRegister"), object: 
    nil)
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoBook"), object: nil)
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoCurrentMainMenu"), 
    object: nil)
    NotificationCenter.default.removeObserver(self, name: 
    NSNotification.Name(rawValue: "gotoEventMenu"), 
    object: nil)
}

如果您在某个需要按照您的节奏释放的对象中创建大量操作,您可以在deinit执行此deinit

The "best answer" should now look more like this

I was not able to edit it

 struct Bank {
static var coinsInBank = 10000
static func vendCoins(money numberOfCoinsToVend: Int) -> Int {
    let CoinsToVend = min(numberOfCoinsToVend, coinsInBank)
    coinsInBank -= CoinsToVend
    return CoinsToVend
}
static func receiveCoins(coins: Int) {
    coinsInBank += coins
}
}

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



print("Balance in Account: \(Bank.coinsInBank)")

 var Player1: Player? = Player(coins: 9000)



  print("Requested Amount to Withdraw: \(Player1!.coinsInPurse)")

  print("Balance After Withdraw: \(Bank.coinsInBank)")

 Player1 = nil

 print("Balance in Account: \(Bank.coinsInBank)")

And output should look like this

Balance in Account: 10000 Requested Amount to Withdraw: 9000 Balance After Withdraw: 1000 Balance in Account: 10000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM