zjsxwc
2021-09-03 14:33:09 +08:00
虽然 swift 没有 gc,也没有析构函数,但 swift 有 deinit 函数
In Swift, destructors are not required, as the memory deallocation is abstracted away and done automatically. However, they are available and known as “deinitializers”, to perform any cleanup that needs to be done just prior to actual deallocation of the object. Deinitializers are optional, and there can be one at most in a class.
In our car example, before we send it to the junkyard, we might want to un-register the vehicle’s license and cancel the insurance:
class Car {
//properties
init(model:String, color:String, vin:Int) {
// init code
}
deinit {
unRegisterLicense() // some function that un-registers the license
cancelInsurance() // some function that cancels the insurance policy
}
}