When should I use deinit?

后端 未结 6 1332
谎友^
谎友^ 2020-12-08 06:55

I came across a function called deinit() while reading The Swift Programming Language guide, but I\'m still wondering why and when we need to implement it since

相关标签:
6条回答
  • 2020-12-08 07:09

    A deinitilizer 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 creat 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 .Most important is Class definition can have at most one deintilizer per class

    0 讨论(0)
  • 2020-12-08 07:10

    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)
    }
    
    0 讨论(0)
  • 2020-12-08 07:11

    If you are creating much of operations in some object which needs to be deallocated at your pace, you can do that in deinit

    0 讨论(0)
  • 2020-12-08 07:18

    from iOS9, removeObserver is called automatically.

    0 讨论(0)
  • 2020-12-08 07:31

    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.

    0 讨论(0)
  • 2020-12-08 07:31

    If your class manages a file handle or a different resource you can close that handle in deinit to ensure that it doesn't live on after the object has been freed.

    0 讨论(0)
提交回复
热议问题