How in Swift to know that struct is deleted from Memory?

为君一笑 提交于 2020-01-02 01:18:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!