Post

Replies

Boosts

Views

Activity

object lifetimes in swift
As a swift noob, and coming from C++, I wonder why the following prints 'BOOM!!': var text = "" class ArrayHandle {     var array = Array()     init() {         text = "success"     }     deinit {         text = "BOOM!!"     } } struct Array { var buffer = Buffer() } struct Buffer { func doStuff() { print(text)} } var buffer = ArrayHandle().array.buffer.doStuff() Apparently the deinit is called before the text is printed... When you change the code to: var handle = ArrayHandle() handle.array.buffer.doStuff() it prints the expected 'success' Is there a way to force developers to use a var to store an object, or is that just a hidden bug that may fail in the future?
3
0
1.5k
Apr ’22