问题
In swift class type has method deinit() in which we can define that instance of class will be removed from memory. How we can know for struct that it will be removed from memory?
For example,
struct Vehicle { ... }
var v: Vehicle? = Vehicle()
v = nil
回答1:
A simple way is the using of a dummy class. Just create an empty class and implement there the deinit(). Then use this class in your struct as member, p.e.
let dummyClass = DummyClass()
Once the structure is released, the deinit() function of the class is called. If not, then you have a memory leak.
来源:https://stackoverflow.com/questions/46842577/how-in-swift-to-know-that-struct-is-deleted-from-memory