[swift-users] Is '_ = x' guaranteed to hold a reference to x?

Mike Ferenduros mike.ferenduros at gmail.com
Thu Jun 29 20:34:25 CDT 2017


I'm doing a RAII sort of thing with an object, and would like to keep it
alive until an completion-block is called (asynchronously).

Is it sufficient to say '_ = x' in the completion-block to keep a live
reference to the object?

I was told that the optimiser is free to discard this line, and thus the
object could be freed prematurely depending on how the code is compiled. If
so, is there an idiomatic way to do this? Or should I just avoid RAII in
Swift?

Thanks!
Mike Ferenduros
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170630/b89ae412/attachment.html>


More information about the swift-users mailing list