As vacawama noted in his answer, an object is created and then destroyed when the object goes beyond.
BUT , this is only true for code executed on the playground. When you run the application on the Simulator or on the device, the object is immediately destroyed. Caution!
I used the following code from vacawamas answer for test:
class ToDoItem {
var title = ""
init(title: String) {
self.title = title
}
deinit {
print("deinit \(title)")
}
}
func test() {
print("test")
_ = ToDoItem(title: "First")
_ = ToDoItem(title: "Second")
print("end test")
}
func callTest() {
print("calling test()...")
test()
print("back from test()")
}
callTest()
(Sorry, I can not comment on another answer, although I, although it was important to share)
source
share